下面的代码返回JavaScript错误:TypeError:响应为null
注意:它在我的本地机器WAMP上运行良好,但在实时LINUX主机上失败。
有什么想法吗?
查询
$(document).ready(function()
{
$("#run").click(function(event)
{
$('#run').hide();
$('#loader').fadeIn(1000);
$.ajax(
{
type : 'POST',
url : 'process.php',
data : 'name=jolly&surname=fish',
dataType : 'json',
success : function(response)
{
$('#loader').stop(true).fadeOut(function ()
{
if (response.status == 'goodjob')
{
$('#tick').fadeIn(1000, function ()
{
$('#script').fadeIn(1000);
});
}
else
{
$('#cross').fadeIn(1000);
}
});
},
error: function (jqXHR, textStatus, errorThrown)
{
alert(textStatus + '---' + errorThrown);
}
});
});
});
HTML
<div id="first">
<img id="run" src="run.png" />
<img id="loader" src="loader.png" style="display:none;" />
<img id="success" src="success.png" style="display:none;" />
<img id="fail" src="fail.png" style="display:none;" />
</div>
<div id="next" style="display:none;">
....
....
</div>
PHP
<?php echo json_encode(array('status' => 'goodjob')); ?>
FIREBUG的详细信息
---标头
Response Headers
Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection Keep-Alive
Content-Length 0
Content-Type text/html; charset=UTF-8
Date Thu, 30 Aug 2012 13:33:41 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Pragma no-cache
Proxy-Connection Keep-Alive
Server Apache/2.2.3 (Red Hat)
X-Powered-By PHP/5.1.6
Request Headers
Accept application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Cache-Control no-cache
Content-Length 27
Content-Type application/x-www-form-urlencoded; charset=UTF-8
Cookie MoodleSession=7uck9j6n2ff7r9vc63tvt85t43; MoodleSessionTest=q0Ywtc2psj; MOODLEID_=%25E2%25C8%2513E%25BD
Host 192.168.10.11
Pragma no-cache
Proxy-Connection keep-alive
Referer http://192.168.10.11/portal/update-database/index.php
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1
X-Requested-With XMLHttpRequest
---后
Parameters application/x-www-form-urlencoded
name jolly
surname fish
Source
name=jolly&surname=fish
从php文件返回
$status = 'goodjob';
echo $status;
在您的Jquery 中
success : function(response)
{
if(response == 'goodjob')
//what you want todo
}
如果它在本地wamp上工作,那么它似乎与js无关。你需要查看你的直播主持人。
确保服务器能够运行php。
确保你不需要
检查服务器上的任何错误日志-任何阻止脚本运行的操作都可能导致此错误。
两件事:
首先,用浏览器点击URL,然后将输出粘贴到JSON linter中,从而自己测试PHP文件:http://jsonlint.com/
我以前遇到过一个问题,我的实时PHP服务器的错误设置与本地服务器不同,所以有时AJAX调用会失败,因为输出会被来自PHP的错误消息污染,无法验证为JSON。
第二,测试你的反应!在$.ajax()
调用的回调中,您只需直接插入并假设response
有一个值。永远不要假设一个你自己没有定义的变量有值,尤其是在javascript中。一个简单的测试,比如:
if (response)
或if (typeof(response)!=='undefined'
会让你省心。