【问题标题】:jquery TypeError: Response is nulljquery TypeError:响应为空
【发布时间】:2012-08-30 13:56:28
【问题描述】:

以下代码返回 JavaScript 错误:TypeError: Response is null

注意:它在我的本地计算机 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 的详细信息

--- 标题

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

【问题讨论】:

  • 你能做一个jsFiddle吗?这将帮助我们更快地解决这个问题。
  • 尝试将您的 php 响应保存在一个简单的变量中
  • $status='goodjob';回声 $status
  • 在 JQuery 中成功函数检查像这样成功:function(response) { if (response == 'goodjob'){}
  • phpinfo() 中的 json 支持未列出,因为我猜这就是问题所在。它在我的本地 php 中启用。

标签: php javascript jquery linux apache


【解决方案1】:

从你的 php 文件返回

$status = 'goodjob';
echo $status;

在你的 Jquery 中

success    : function(response)
{
   if(response == 'goodjob')
   //what you want todo
}

【讨论】:

  • 实际上应该是if (response.status=='goodjob')。详情见问题
  • 对不起,你是对的,我误读了你的问题。如果它是问题中的数组('status'=>'goodjob'),那么它应该是 response.status 但你的例子是正确的。对不起:)
【解决方案2】:

如果它适用于您本地的 wamp,这似乎与 js 无关。您需要检查您的直播主机。

确保服务器能够运行php。

确保您不需要

检查服务器上的任何错误日志 - 阻止脚本运行的任何内容都可能导致此错误。

【讨论】:

    【解决方案3】:

    两件事:

    首先,通过使用浏览器点击 URL 并在此处将输出粘贴到 JSON linter 来测试您的 PHP 文件:http://jsonlint.com/

    在我的实时 PHP 服务器与本地服务器的错误设置不同之前,我遇到过一个问题,因此有时 AJAX 调用会失败,因为输出会被来自 PHP 的错误消息污染并且无法验证为 JSON。

    第二,测试你的反应! 在您的$.ajax() 调用的回调中,您直接插入并假设response 有一个值。永远不要假设您没有定义自己的变量具有值,尤其是在 javascript 中。 一个简单的测试,例如:

    if (response)if (typeof(response)!=='undefined' 可以让您省去很多麻烦。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-04
      • 2013-05-03
      • 1970-01-01
      • 1970-01-01
      • 2015-12-24
      • 2011-03-05
      • 2016-05-09
      • 1970-01-01
      相关资源
      最近更新 更多