Use JSONP.
jQuery:
$.ajax({
url:"testserver.php",
dataType: 'jsonp', // Notice! JSONP <-- P (lowercase)
success:function(json){
// do stuff with json (in this case an array)
alert("Success");
},
error:function(){
alert("Error");
}
});
PHP:
<?php
$arr = array("element1","element2",array("element31","element32"));
$arr['name'] = "response";
echo $_GET['callback']."(".json_encode($arr).");";
?>
The echo might be wrong, it's been a while since I've used php. In any case you need to output callbackName('jsonString')
notice the quotes. jQuery will pass it's own callback name, so you need to get that from the GET params.
And as Stefan Kendall posted, $.getJSON() is a shorthand method, but then you need to append 'callback=?'
to the url as GET parameter (yes, value is ?, jQuery replaces this with its own generated callback method).
You are having a server with a public IP and apache is running on it.Now you want to host your applications on LAN and also want them to be accessible on internet the important part is these applications are still running on the machines on LAN.
|--------------192.168.1.3
| (internal3.example.com)
|
|--------------192.168.1.4
| (internal4.example.com)
(Public IP ) |
A--------------|
(reverse proxy server) |
(192.168.1.25) |
example.com |
|--------------192.168.1.1
| (internal1.example.com)
|
|--------------192.168.1.2
| (internal2.example.com)
I am using Ubuntu to host Apache the vhost definition in case of Debian based systems the definiton of websites is done on
/etc/apache2/sites-enabled/*.conf
where *conf corresponds to
internal1.conf internal2.conf internal3.conf internal4.conf
The vhost definition of each of these sites will be as follows
/etc/apache2/sites-enabled/internal1.example.conf
<virtualhost *:80>
ServerAdmin webmaster@localhost
ServerName internal1.example.com
ProxyRequests off
<proxy *>
Order deny,allow
Allow from all
</proxy >
ProxyPass / http://192.168.1.1/
ProxyPassReverse / http://192.168.1.1/
</VirtualHost>
/etc/apache2/sites-enabled/internal2.example.conf
<virtualhost *:80>
ServerAdmin webmaster@localhost
ServerName internal2.example.com
ProxyRequests off
<proxy *>
Order deny,allow
Allow from all
</proxy >
ProxyPass / http://192.168.1.2/
ProxyPassReverse / http://192.168.1.2/
</VirtualHost >
/etc/apache2/sites-enabled/internal3.example.conf
<virtualhost *:80>
ServerAdmin webmaster@localhost
ServerName internal3.example.com
ProxyRequests off
<proxy *>
Order deny,allow
Allow from all
</proxy >
ProxyPass / http://192.168.1.3/
ProxyPassReverse / http://192.168.1.3/
</VirtualHost >
/etc/apache2/sites-enabled/internal4.example.conf
<virtualhost *:80>
ServerAdmin webmaster@localhost
ServerName internal4.example.com
ProxyRequests off
<proxy *>
Order deny,allow
Allow from all
</proxy>
ProxyPass / http://192.168.1.4/
ProxyPassReverse / http://192.168.1.4/
</VirtualHost>
Note in all of the above vhost definitions I have dropped the options of Log files.
So if you apply to a production server add them in each of the vhost file.
Above is just to give you a clear cut example as how it can be working.
I run a very complex Apache setup so above is just a small example to help you.
Now coming to Ajax part of your question
in chrome press Ctrl+Shift+I you will see where exactly the application is broken,
it will give you some clue, (issue the request from a machine different from the machine on which you are developing web application)
also if you can look at apache logs if the request from http://sample
page which has ajx api actually reached your apache server that will give you more hint, if the proxy is forwarding your request correctly, post the HTTP HEADERS by using some tool in firefox like live_http in condition when there was no request and the condition when the request was made by the application that way observing the headers one can help you if the request reached the server behind the reverse proxy,also check the logs of server which is running reverse proxy if the request from web reached it or not and if the request reached what is the URL that was requested.This will give you a clue,
and for development purpose in your .conf files disable the rewrite rules for some time to test ,do it one by one.
Best Answer
I found a working solution:
Enable:
Put this in the main section of your configuration (or desired virtual host, if using Apache virtual hosts):
So I guess I can't put it in
.htaccess
or I had to setProxyPreserveHost On
. I putInclude conf/extra/
in thehttpd.conf
file and created thehttpd-proxy.conf
file and put the script above in it. Restarted apache and it's working!