【问题标题】:JSON data response from PHP server is empty来自 PHP 服务器的 JSON 数据响应为空
【发布时间】:2011-01-21 02:49:36
【问题描述】:

我很难弄清楚这一点。似乎无论我尝试什么,PHP 总是最终返回一个空数组。这是我的主文件(index.php)的代码:

<script language="javascript" type="text/javascript">

$(document).ready(function(){

  $(".ajaxlink").click(function() {
    callServer();
    return false; //Stop link from redirecting
  });

});

var test = { "testName": "testValue" }
var testJSON = JSON.stringify(test);

function updatePage(data) {
  document.getElementById("testDiv").innerHTML = data;
}

function callServer() {
 $.ajax({
   type: "POST",
   url: "ajax/server.php",
   data: testJSON,
   success: function(data) {
     updatePage(data);
   },
   //Upon error, output message containing a little info on what went wrong
   error: function (XMLHttpRequest, textStatus, errorThrown) {
     alert('An Ajax error occured\ntextStatus = ' + textStatus + '\nerrorThrown = ' + errorThrown + '\nstatus = ' + XMLHttpRequest.status);
   }
 });
}

</script>

<div id="testDiv">Something here</div>

<a href="test1.htm" class="ajaxlink">Link!</a> <br>

当您单击“链接!”时,这基本上会运行 callServer() 函数。然后它将测试 json 数据,即 { "testName": "testValue" } 发送到 server.php。 Firebug 报告 json-data 确实发送到了 server.php。

我的 server.php 看起来像这样:

<?php

print_r($_POST);

?>

这会在 testDiv 中返回以下内容:

Array
(
)

.ajax 函数中的数据类型没有定义,所以无论 server.php 文件输出什么输出,它都应该是可读的。所有必要的库(json、jquery)也包含在我的文档中。我在 Apache 2.2 和 PHP 5.3.1 上运行它,但它在我的网络服务器(它是数千个网站的主机)上显示相同。请求头中使用的内容类型是 'application/x-www-form-urlencoded; charset=UTF-8' 这样应该可以正常工作。

感谢您的宝贵时间。 最好的祝福 疼痛

【问题讨论】:

    标签: php jquery ajax json


    【解决方案1】:

    我不确定您的 PHP 脚本输出是 JSON 格式的。

    如果您使用的是较新版本的 PHP(您是),您将可以访问 json_encode 和 json_decode 函数。而不是这样做:

    print_r($_POST);
    

    试试:

    print json_encode($_POST);
    

    如果您的 PHP 版本没有这些函数,您可以使用 Zend 框架中的 Zend_Json 类等库,以便在输出 PHP 变量之前将它们编码为 JSON。

    当它返回时,它将是一个 JSON 格式的字符串。在 jQuery.ajax 调用中设置 dataType 应该将其评估为 JS 对象。如果不是,您要么必须调用 Javascript eval 函数,要么(最好)使用 JSON.parse(data)。

    【讨论】:

    • 我将您的代码更正为 $_POST 而不是 $_POST。那只是输出以下'[]'
    【解决方案2】:

    我认为您以错误的方式发送数据。要么发送testName=testValue 之类的字符串,要么将test 中的值直接分配给.ajax()data 参数,并且不要使用stringify 方法。

    因为,如果你使用stringify,实际发送的数据将是(我假设,这里我不确定):

    '{ "testName": "testValue" }'

    但这不是一个有效的参数字符串。

    应该是形式

    'testName=testValue'

    所以直接使用test.ajax()会将对象转换成合适的字符串:

    function callServer() {
     $.ajax({
       type: "POST",
       url: "ajax/server.php",
       data: test,
       success: function(data) {
         updatePage(data);
       },
       //Upon error, output message containing a little info on what went wrong
       error: function (XMLHttpRequest, textStatus, errorThrown) {
         alert('An Ajax error occured\ntextStatus = ' + textStatus + '\nerrorThrown = ' + errorThrown + '\nstatus = ' + XMLHttpRequest.status);
       }
     });
    }
    

    【讨论】:

    • 在这里同意...您不需要字符串化。只需将数据作为 JSON 发送。将该测试变量作为数据而不是 testJSON。
    • Firebug 说我正确地发送了 JSON 数据。我的 js 中的“测试”也像一个对象。
    【解决方案3】:

    使用 firefox 和 Live Http Headers 扩展。
    有了这个,您将能够准确地看到问题所在,
    php或js代码。

    live http headers

    【讨论】:

    • 这会吐出以下内容:pastebin.org/97270 这对我来说似乎没问题
    • 不,它应该看起来像这样:从 {"testName":"testValue"} 女巫在我看来是错误的 testName=testValue
    猜你喜欢
    • 2017-11-18
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    相关资源
    最近更新 更多