开发者

AJAX randomly returns response as NULL

开发者 https://www.devze.com 2023-03-27 17:22 出处:网络
Here is the Widget i created - http://resources.bigrock.in/affiliate/widgets/Domain_Search_widget-300x250.html. It works fine and as expected ..mostly. The problem is, sometimes, randomly the Ajax re

Here is the Widget i created - http://resources.bigrock.in/affiliate/widgets/Domain_Search_widget-300x250.html. It works fine and as expected ..mostly. The problem is, sometimes, randomly the Ajax response is null(i.e. 'data is null' is the error i get). I've been trying to figure the cause of this problem. Any help will greatly be appreciated.

and here are the contents of the PHP file

header("Content-Type: application/json");
//header("Content-Type: text/javascript");

$domain_name = $_GET['domain_name'];
$tld = $_GET['tld'];

$开发者_如何学Pythontext = file_get_contents("http://www.some.com/some.php?action=caajax&domain_name=$domain_name&tld=$tld");
echo $text;

earlier i was using "Content-Type: text/javascript" as you can see. But the occurrence of the error was much more. So i tried "Content-Type: application/json" while this greatly reduced the occurrence of the error it did not eliminate it.

I'm still learning a lot of these stuffs.. So any idea why this is happening??

Edit: On Codo's advice i studied the ajax profile using firebug. On a normal successful ajax call there were- params | headers | response | json

But on error there was - params | headers | rewponse | XML

here is the image - http://resources.bigrock.in/affiliate/widgets/img/error_headers.jpg


Your problem is that you are not encoding your response as JSON. Whatever you set in the header needs to be what you echo the content as. Also, text/javascript isn't really a good header to use. If you are just trying to echo out the html from that link, it should be text/html.

Here is the code for JSON:

header("Content-Type: application/json");
//header("Content-Type: text/javascript");

$domain_name = $_GET['domain_name'];
$tld = $_GET['tld'];

$text = file_get_contents("http://www.some.com/some.php?action=caajax&domain_name=$domain_name&tld=$tld");
echo json_encode($text);
0

精彩评论

暂无评论...
验证码 换一张
取 消