php - API stop supporting JSONP -
i made simple api in php. api echo's object using json_encode.
per example:
echo json_encode($obj);
i using jquery retrieve information api. when testing script , api, both running on same server. , worked fine.
an example of jquery:
$.ajax({ url: "php/api/test.php" datatype: "json", type: "post", succes: function(data) { console.log("success"); }, error: function(response) { console.log("error"); } });
now worked fine. since api running on external server not work anymore (yes changed url correct one).
i had alter api , javascript in order use jsonp before jquery receive information api.
my jquery is:
$.ajax({ url: "http://externalserver/php/api/test.php" datatype: "jsonp", type: "post", succes: function(data) { console.log("success"); }, error: function(response) { console.log("error"); } });
the php return in order work:
echo $_request['callback'] . '(' . json_encode($obj) . ')';
now rather use json in stead of jsonp.
i understood need alter php file let api accept incomming requests external script mine.
can tell me need do?
you need set parameter in header allows crossoriginressourcesharing (cors) or else browser block calls due same-origin policy (sop). if set allow-origin-response-header within php, should work intended.
putting
header('access-control-allow-origin: *');
at top of test.php
fix problem. (or preferably replace *
specific domain calls come from).
Comments
Post a Comment