在 javascript 和 PHP 之间发送 JSON 字符串的正确方法是什么?
What is the correct way to send a JSON string between javascript and PHP?
我正在尝试使用 XMLHttpRequest 和 Json 字符串从 javascript 发送少量数据到 PHP 脚本来处理它并 return 一些响应再次以 Json 字符串的形式出现,但我已经 运行 遇到大量问题和不同的方法,它们无法正常协同工作,以下是目前有效的方法:
客户端
json_string = '{"foo":"1","bar":"2"}';
var r = new XMLHttpRequest();
r.open('post', 'script.php', true);
r.setRequestHeader('Content-type','application/json; charset=utf-8');
r.setRequestHeader('Content-length', json_string.length);
r.setRequestHeader('Connection', 'close');
r.onload = function () {
console.log(this.responseText);
};
r.send(json_string);
服务器
$json = file_get_contents('php://input');
echo $json;
没有比这更简单的了,但是我收到了这个警告:
[09-Jan-2015 15:50:03 America/Mexico_City] PHP Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0
[09-Jan-2015 15:50:03 America/Mexico_City] PHP Warning: Cannot modify header information - headers already sent in Unknown on line 0
当然,响应文本如下所示:
Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0
Warning: Cannot modify header information - headers already sent in Unknown on line 0
{"foo":"1","bar":"2"}"
我做错了什么?为什么 PHP 抱怨已经发送 headers?
我正在使用 PHP 5.6.
您需要编辑 php.ini 文件并设置 always_populate_raw_post_data = -1
。
您的代码运行良好。 $json = file_get_contents('php://input');
正确。
您刚刚收到警告,因为您的 PHP 已更新,您需要为该新版本更新 php.ini 文件。
我查看了我的一些旧代码,它们确实可以 Ajax 而没有 Jquery。在 POST 参数中发送 Json 与您已经在做的没有什么不同。
你真的只需要改变你正在设置的headers,并给它一个参数名称:
json_string = '{"foo":"1","bar":"2"}';
var r = new XMLHttpRequest();
r.open('post', 'script.php', true);
r.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
r.onload = function () {
console.log(this.responseText);
};
r.send('json='+encodeURIComponent(json_string));
然后,当然是 PHP:
$_POST['json']
编辑:添加了 encodeURIComponent()
。
我正在尝试使用 XMLHttpRequest 和 Json 字符串从 javascript 发送少量数据到 PHP 脚本来处理它并 return 一些响应再次以 Json 字符串的形式出现,但我已经 运行 遇到大量问题和不同的方法,它们无法正常协同工作,以下是目前有效的方法:
客户端
json_string = '{"foo":"1","bar":"2"}';
var r = new XMLHttpRequest();
r.open('post', 'script.php', true);
r.setRequestHeader('Content-type','application/json; charset=utf-8');
r.setRequestHeader('Content-length', json_string.length);
r.setRequestHeader('Connection', 'close');
r.onload = function () {
console.log(this.responseText);
};
r.send(json_string);
服务器
$json = file_get_contents('php://input');
echo $json;
没有比这更简单的了,但是我收到了这个警告:
[09-Jan-2015 15:50:03 America/Mexico_City] PHP Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0
[09-Jan-2015 15:50:03 America/Mexico_City] PHP Warning: Cannot modify header information - headers already sent in Unknown on line 0
当然,响应文本如下所示:
Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0
Warning: Cannot modify header information - headers already sent in Unknown on line 0
{"foo":"1","bar":"2"}"
我做错了什么?为什么 PHP 抱怨已经发送 headers?
我正在使用 PHP 5.6.
您需要编辑 php.ini 文件并设置 always_populate_raw_post_data = -1
。
您的代码运行良好。 $json = file_get_contents('php://input');
正确。
您刚刚收到警告,因为您的 PHP 已更新,您需要为该新版本更新 php.ini 文件。
我查看了我的一些旧代码,它们确实可以 Ajax 而没有 Jquery。在 POST 参数中发送 Json 与您已经在做的没有什么不同。
你真的只需要改变你正在设置的headers,并给它一个参数名称:
json_string = '{"foo":"1","bar":"2"}';
var r = new XMLHttpRequest();
r.open('post', 'script.php', true);
r.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
r.onload = function () {
console.log(this.responseText);
};
r.send('json='+encodeURIComponent(json_string));
然后,当然是 PHP:
$_POST['json']
编辑:添加了 encodeURIComponent()
。