I created a php file to return data encoded with JSON and then used getJSON to read it. I am attempting to get info from one source and distribute it to multiple areas. I have changed the content-type also allowing all access with headers. At this point I am stuck. Any ideas how to get this working?
PHP
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');
header('Access-Control-Allow-Origin: *');
$search_terms = curlData();
$json = array("term" => $search_term);
echo $_GET['callback'] . "(" . json_encode($json) . ");";
the output for this php file is as follows:
({"term":"sandwich city"});
Here is the jQuery I am using to retrieve the json info
$.getJSON("http://MYWEBSITE.com/terms.php?callback", function(data) {
alert(data);
});
({"term":"sandwich city"});is not valid JSONP. You are missing the function name.Add
=?to your URL, so that jQuery automatically generates a function to handle the response:From the documentation:
Btw, if you send JSONP as response, setting the content type of the response to
application/jsonis not correct. The response is simply JavaScript.Furthermore, as you specified the
Access-Control-Allow-Originheader, you don’t have to use JSONP at all, you can make a normal AJAX call and return JSON.