从PHP服务器JSON数据响应为空 [英] JSON data response from PHP server is empty

查看:203
本文介绍了从PHP服务器JSON数据响应为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很难搞清楚这一个。好像无论我怎么努力,PHP最终总是返回一个空数组。这是我的主要文件的code(的index.php):

I'm having a hard time figuring this one out. Seems like no matter what I try, PHP always ends up returning an empty array. Here's the code of my main file(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数据,即{测试名:testValue}以server.php。 Firebug的报告说,JSON数据确实发送到server.php。

This basically runs the callServer() function when you click the "Link!". It then sends the test json data, that is { "testName": "testValue" } to server.php. Firebug reports that the json-data is indeed sent to the server.php.

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

My server.php looks like this:

<?php

print_r($_POST);

?>

这将返回在testDiv如下:

This returns the following in the testDiv:

Array
(
)

在阿贾克斯功能的数据类型没有定义,所以无论输出server.php文件吐出来的,它应该是可读的。所有必要的库(JSON,jQuery的)都包含在我的文件也是如此。我在Apache 2.2和PHP 5.3.1在我的网络服务器上运行,但它显示了相同的(这是一个主机数千个网站)。内容类型的请求头采用的是应用/的X WWW的形式,urlen codeD;字符集= UTF-8,这样应能正常工作。

The datatype in the .ajax function is not defined, so whatever output the server.php file spits out, it should be readable. All the necessary libraries(json, jquery) are included in my document as well. I'm running this on Apache 2.2 and PHP 5.3.1, but it shows the same on my webserver (which is a host for thousands of websites). The content-type used in the request-header is 'application/x-www-form-urlencoded; charset=UTF-8' so that should work correctly.

感谢您的时间。 诚挚的问候 索伦

Thanks for your time. Best regards soren

推荐答案

我觉得你发送的数据错误的方式。要么你发送类似的字符串测试名= testValue 或您指定的值测试直接将参数>阿贾克斯(),不使用字符串化方法。

I think you send the data in a wrong way. Either you send a string like testName=testValue or you assign the value in test directly to the data parameter of .ajax() and don't use the stringify method.

由于,如果你使用字符串化,实际发送的数据将是(我想,我不知道这里):

Because, if you use stringify, the actual sent data will be (I assume, I am not sure here):

'{测试名:testValue}

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

but this is not a valid parameter string.

这应该是形式

测试名= testValue'

所以,使用测试直接, 阿贾克斯() 将对象转换成相应的字符串:

So use test directly, .ajax() will convert the object into an appropriate string:

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);
   }
 });
}

这篇关于从PHP服务器JSON数据响应为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆