【问题标题】:dataType: "json" won't work数据类型:“json”不起作用
【发布时间】:2012-04-16 15:04:12
【问题描述】:

我正在尝试使用数组中的 json 将多个变量从 php 文件发送回 ajax。 php 文件中的代码可以完美运行,并且可以正常使用我的数据库。但是,一旦我在 ajax 中添加 dataType: "json",php 文件中就没有任何反应了。我用谷歌搜索了一下,有人提到这可能是浏览器问题,但到目前为止,它在 firefox、chrome 或 IE 中都不起作用。我正在使用最新版本的 jQuery。

这就是php内部发生的事情:

<?php
//Create variables and update database

echo json_encode(array("id" => "$realid", "un" => "$username", "date" => "$date"));
?>

这是 ajax 代码:

.ajax(
{
   url: 'UpdateComments.php',
   type: 'POST',
   dataType: "json",
   data: 
   {
      type: "add",
      comment: $("#comment").val(),
      id: videoID  
   },
   success: function (data) 
   {
       //Get the data variables from json and display them on page
   }
});

我对此一无所知,任何建议将不胜感激!

【问题讨论】:

  • 检查 firebug / net 面板中的 ajax 响应并查看来自您的服务器的内容。
  • 一个 contentType HTTP 标头可能会有所帮助
  • 我怀疑返回的不是严格的 JSON - 检查 PHP 之前或之后的空格,或文件中不应该存在的任何其他内容(提示:在 json_encode 之后使用'die' )。当你没有在 JQuery 中指定 dataType 时,成功 = 得到响应。当你这样做时,成功 = 得到一个 VALID 响应......另外检查你的 PHP 版本,我似乎记得 json_encode 在早期版本中被窃听了。

标签: php ajax json


【解决方案1】:

常见问题是浏览器在 JSON 之前打印“其他内容”,无论是可读还是 不可读(不可见)字符。尝试做这样的事情:

<?php
//at the very beginning start output buffereing
ob_start();

// do your logic here

// right before outputting the JSON, clear the buffer.
ob_end_clean();

// now print
echo json_encode(array("id" => $realid, "un" => $username, "date" => $date));
?>

现在,所有补充数据(在 JSON 之前)都将被丢弃,您应该让它正常工作...

【讨论】:

    【解决方案2】:

    我相信如果你使用 dataType,你应该使用 contentType,“JSON 的官方 Internet 媒体类型是 application/json”。

    .ajax(
    {
     url: 'UpdateComments.php',
     type: 'POST',
     contentType: "application/json",//note the contentType defintion
     dataType: "json",
     data: 
     {
       type: "add",
       comment: $("#comment").val(),
       id: videoID  
     },
     success: function (data) 
     {
       //Get the data variables from json and display them on page
     }
    });
    

    【讨论】:

      【解决方案3】:

      很容易忘记echovar_dump(),您过去可能一直在使用它们来测试脚本的工作方式。

      在我自己的脚本中,我有一个 var_dump(),我忘记了,它没有使用 JSON_encode 编辑的文本并将纯文本发送到浏览器。这打破了 dataType:"json" 要求并导致成功功能不起作用。

      我花了一段时间才找到问题,因为我只有 ctrl+f(ed) 用于 echo,而忘记了流浪者 var_dump()

      printf 可能是另一个嫌疑人。

      【讨论】:

      • 是的,我在 SQL 上有一个回显,所以我可以看到错误。谢谢!
      • 这个.. 控制器中某处的一些 print_r()
      【解决方案4】:

      如果你在 jQuery 中设置 dataType,那实际上设置了 Content-Type 标头属性。也许,在您的 PHP 脚本中,您需要将此 MIME 类型声明为已接受。当您发出请求时,您是否注意到代码甚至进入了 PHP 脚本?如果它在 Firefox、Chrome 或 IE 中不起作用,我怀疑这是浏览器问题。

      为了更好地了解您的 AJAX 请求,订阅 ajaxBeforeSend(不确定事件名称是否正确,检查 jQ 文档)事件并记录 xhr 对象。

      【讨论】:

        【解决方案5】:

        如果它给您带来问题,我不会使用 dataType,而且我个人之前也没有使用对象作为数据值,这可能与它有关吗?

        无论如何,我已经调整了主要的 ajax 例程,我希望这会有所帮助。

        $.ajax(
        {
           url: 'UpdateComments.php',
           type: 'POST',
           data: 
           {
              type: "add",
              comment: $("#comment").val(),
              id: videoID  
           },
           success: function (response) 
           {
               //Get the data variables from json and display them on page
               var data = $.parseJSON(response);
               alert(data.id);
           }
        });
        

        【讨论】:

          【解决方案6】:

          尝试将错误处理程序定义为 $.ajax 调用的一部分

          $.ajax({
            ...,
            error: function(xml, error) {
              console.log(error);
            }
          });
          

          然后检查您的调试控制台是否有任何可以帮助您诊断问题的错误。

          【讨论】:

            【解决方案7】:
                    $.ajax({
                
                   url: '/route/',
                   type: 'POST',
                   dataType: "json",
                   data: 
                   {
                      type: "add",
                      comment: $("#comment").val(),
                      id: videoID  
                   },
                   success: data => {console.log(data);}
                
                });
                
                <?php
                
            ob_start();
                
            ob_end_clean();
                
            echo json_encode(array("id" => "$realid", "un" => "$username", "date" => "$date"));
                ?>
                
                    
            

            【讨论】:

              猜你喜欢
              • 2020-10-03
              • 2019-10-14
              • 1970-01-01
              • 1970-01-01
              • 2017-07-11
              • 1970-01-01
              • 1970-01-01
              • 2011-08-19
              • 1970-01-01
              相关资源
              最近更新 更多