jquery - Cross Origin Resource Sharing using AJAX methods and PHP -
i started work on calls php file present in different server. aware of cors essential cross domain requests. have been trying call file through ajax methods refering other websites , tutorials , have seen discussions find solution not working me. please help.
here calling method:
$.ajax({ type: "get", url: "http://cs-server.usc.edu:27952/responseprog.php?callback=?", //relative or absolute path response.php file datatype: "jsonp", data: datainput, jsonp:'jsoncallback', crossdomain:true, success: function(data) { jsonobj = jquery.parsejson(data); contentprovider("#rtrn"); if(jsonobj.ack != "no results found") { var paginate=setpager(0); $("#pgn").html(paginate); } }, error: function() { $("#rtrn").html("data not retrieved successfully"); } }); here php code snippet:
<?php #code data processing... $rsltjson = json_encode($result,json_unescaped_slashes); echo $_get['jsoncallback']."(".$rsltjson.");"; ?> i trying accomplish using jsonp. should have headers? there errors in code?....how can accomplish this? datainput serialized form of form data
the cors way
you need put appropriate header in php script , output json:
<?php header('access-control-allow-origin: *'); // rest of code // output json echo $rsltjson; ?> then using xmlhttprequest/ajax call should retrieve data fine json without resorting jsonp.
the jsonp way
since whole point of jsonp bypass cross-domain restrictions, calling jsonp resource xmlhttprequest/ajax, method in cross-domain security applied (presumably), useless.
jsonp works injecting code directly page, calling function defined, why jsonp url takes argument. therefore, correct way call jsonp url this:
<script> function mydatafunc(data) { jsonobj = jquery.parsejson(data); contentprovider("#rtrn"); if(jsonobj.ack != "no results found") { var paginate=setpager(0); $("#pgn").html(paginate); } } </script> <script src="http://cs-server.usc.edu:27952/responseprog.php?jsoncallback=mydatafunc"></script> the jsonp url return looks this:
mydatafunc({"a":"fhsfg","b":"qfdgers","c":"difgij"}); since included script, executed directly in page, calling function mydatafunc() defined earlier.
also note php file use parameter jsoncallback while javascript calls url parameter callback, not work.
finally, use jquery.parsejson(), produces error code:
syntaxerror: json.parse: unexpected character @ line 1 column 2 of json data the reason can found in jquery docs:
jquery.parsejson( json )
description: takes well-formed json string , returns resulting javascript value.
passing in malformed json string results in javascript exception being thrown.
your php script feeds callback json object
{"a":"fhsfg","b":"qfdgers","c":"difgij"} rather string representing json object
'{"a":"fhsfg","b":"qfdgers","c":"difgij"}' note surrounding quotes, makes data string. fix in php adding quotes around data:
echo $_get['jsoncallback']."('".$rsltjson."');"; obviously if json data contains single quotes, have escape them.
Comments
Post a Comment