以下代码返回JavaScript错误: TypeError:响应为空
注意:它在我的本地计算机WAMP上运行正常但在现场LINUX主机上失败。
有什么想法吗?
JQUERY
$(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
--- HEADERS
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
--- POST
Parameters application/x-www-form-urlencoded
name jolly
surname fish
Source
name=jolly&surname=fish
答案 0 :(得分:0)
如果它适用于您当地的wamp,这似乎与js无关。你需要检查你的直播主机。
确保服务器能够运行php。
确保您不需要
检查服务器上的任何错误日志 - 阻止脚本运行的任何内容都可能导致此错误。
答案 1 :(得分:0)
两件事:
首先,通过使用浏览器点击URL并在此处将输出粘贴到JSON linter来自己测试PHP文件:http://jsonlint.com/
在我的实时PHP服务器具有与本地错误设置不同的错误设置之前,我遇到了一个问题,因此有时AJAX调用会失败,因为输出会被来自PHP的错误消息污染并且无法验证为JSON。
第二,测试你的反应!
在$.ajax()
来电的回调中,您只需直接进入并假设response
有值。永远不要假设你自己没有定义的变量有值,特别是在javascript中。
一个简单的测试,如:
if (response)
或if (typeof(response)!=='undefined'
会让您头疼。
答案 2 :(得分:0)
从您的php文件返回
$status = 'goodjob';
echo $status;
在你的Jquery
中success : function(response)
{
if(response == 'goodjob')
//what you want todo
}