Here is one method to get resolve the error AJAX No 'Access-Control-Allow-Origin' header is present on the requested resource when making ajax request to different server.
To resolve this you need to create simple proxy php in the same domain with following content.
And make the ajax request to proxy.php in same domain and pass the original url as argument.
To resolve this you need to create simple proxy php in the same domain with following content.
header('Content-type: application/json');
$url=$_GET['url'];
if (is_string($url)) {
$url = urldecode($url);
}
$json=file_get_contents($url);
echo $json;
And make the ajax request to proxy.php in same domain and pass the original url as argument.
var url = "http://different_domain.php";
url = encodeURIComponent(url);
url = 'proxy.php?url='+url;
$.ajax({ url: url,type: "GET",data: { }, dataType: "json",
success: function (jsonStr) {
// resposne code here
},
error: function(jqXHR, textStatus, errorThrown){
//error managment here
}
});
No comments:
Post a Comment