The Way to Programming
The Way to Programming
I’m writing a website that gets data from another domain. The purpose is that people can lookup when there are roadblocks ’cause of work and events. Everything works, except the cross-domain request.
I’ve searched the web for “simple” php-proxies, but whatever I do, it doesn’t work on my site. Also, keep in mind, I’m completely new to PHP and working with ajax.
Here’s the code:
PHP-proxy, courtesy of Yahoo!:
Here’s the code for my script:
function searchWorkAssignments(datums, nisCode)
{
$("#Werkopdrachten").html("");
$("#detail_info").html("");
clearMap();
var nisCode = 31043;
var path = encodeURIComponent('DataService/GipodService.svc/GetWerkopdrachtenAsJson?starttijdstip=' + datums[0] + '&eindtijdstip=' + datums[1] + '&startpositie=1&maxrecords=25&niscode=' + nisCode);
var url = 'proxy.php?url=' + path;
if (nisCode.length !== 0) {
$.ajax({
url: url,
cache: false,
type: 'GET',
success: function (data, status, xhr) {
alert(data.length);
showResults(data);
},
error: function (xhr, status, error) {
alert("Fout bij het laden van werkopdrachten!");
}
});
}
}
Can someone please show me what I’m doing wrong and maybe point to the right direction?
My question is are cross domain requests allowed? I know they are blocked in JS for security reasons. Look up CORS =)
Making cross-domain calls via a proxy is a simple solution, assuming you do it right. Basically, you need 3 things: the target URL, and parameters and the “verb” (GET, POST, etc.). Your code is fine, except you’re passing all this stuff as var url = ‘proxy.php?url=’ + path, yet you’re trying to access it in your PHP as $_POST[‘yws_path’]. The fix is simple – either change your JS to ‘proxy.php?yws_path=’ or change your PHP to $_POST[‘url’] and $_GET[‘url’].
Sign in to your account