I made a simple API in PHP. This API echo's an object using json_encode.
Per example:
echo json_encode($obj);
I am using jQuery to retrieve the information from this API. When testing the script and the API, they where both running on the same server. And everything worked fine.
An example of the jQuery:
$.ajax({
url: "php/api/test.php"
dataType: "json",
type: "POST",
succes: function(data) {
console.log("success");
},
error: function(response) {
console.log("error");
}
});
Now this worked fine. However since the API is running on an external server this does not work anymore (yes I changed the url to the correct one).
I had to alter the API and the javascript in order to use JSONP Before jQuery would finally receive the information from the API.
My jQuery now 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 will return this in order to work:
echo $_REQUEST['callback'] . '(' . json_encode($obj) . ')';
Now I actually would rather use JSON in stead of JSONP.
I understood that I need to alter the PHP file to let the API accept incomming requests from an external script like mine.
Can you tell me what it is I need to do?