将 JSON 数据从 Javascript 发送到 PHP? [英] Send JSON data from Javascript to PHP?

查看:47
本文介绍了将 JSON 数据从 Javascript 发送到 PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 JSON 数据从浏览器中的 Javascript 发送到服务器并让 PHP 在那里解析它?

How can I send JSON data from Javascript in the browser, to a server and have PHP parse it there?

推荐答案

我在这里得到了很多信息,所以我想发布一个我发现的解决方案.

问题: 从浏览器上的 Javascript 获取 JSON 数据到服务器,并让 PHP 成功解析它.

The problem: Getting JSON data from Javascript on the browser, to the server, and having PHP successfully parse it.

环境: Windows 浏览器 (Firefox) 中的 Javascript.LAMP 服务器作为远程服务器:Ubuntu 上的 PHP 5.3.2.

Environment: Javascript in a browser (Firefox) on Windows. LAMP server as remote server: PHP 5.3.2 on Ubuntu.

什么有效(版本 1):
1) JSON 只是文本.特定格式的文本,但只是一个文本字符串.

2) 在 Javascript 中,var str_json = JSON.stringify(myObject) 给我 JSON 字符串.

3)我在Javascript中使用AJAX XMLHttpRequest对象向服务器发送数据:

What works (version 1):
1) JSON is just text. Text in a certain format, but just a text string.

2) In Javascript, var str_json = JSON.stringify(myObject) gives me the JSON string.

3) I use the AJAX XMLHttpRequest object in Javascript to send data to the server:

request= new XMLHttpRequest()
request.open("POST", "JSON_Handler.php", true)
request.setRequestHeader("Content-type", "application/json")
request.send(str_json)
[... code to display response ...]

4) 在服务器端,读取JSON字符串的PHP代码:

4) On the server, PHP code to read the JSON string:

$str_json = file_get_contents('php://input');

这会读取原始 POST 数据.$str_json 现在包含来自浏览器的确切 JSON 字符串.

This reads the raw POST data. $str_json now contains the exact JSON string from the browser.

什么有效(版本 2):
1)如果我想使用 "application/x-www-form-urlencoded" 请求头,我需要创建一个 "x=y&a=b[etc]" 这样当 PHP 得到它时,它就可以把它放在 $_POST 关联数组中.因此,在浏览器中的 Javascript 中:

What works (version 2):
1) If I want to use the "application/x-www-form-urlencoded" request header, I need to create a standard POST string of "x=y&a=b[etc]" so that when PHP gets it, it can put it in the $_POST associative array. So, in Javascript in the browser:

var str_json = "json_string=" + (JSON.stringify(myObject))

当我通过 AJAX/XMLHttpRequest 发送 str_json 时,PHP 现在可以填充 $_POST 数组,就像上面的版本 1 一样.

显示$_POST['json_string']的内容会显示JSON字符串.对带有 json 字符串的 $_POST 数组元素使用 json_decode() 将正确解码该数据并将其放入数组/对象中.

PHP will now be able to populate the $_POST array when I send str_json via AJAX/XMLHttpRequest as in version 1 above.

Displaying the contents of $_POST['json_string'] will display the JSON string. Using json_decode() on the $_POST array element with the json string will correctly decode that data and put it in an array/object.

我遇到的陷阱:
最初,我尝试发送带有 application/x-www-form-urlencoded 标头的 JSON 字符串,然后尝试立即从 PHP 中的 $_POST 数组中读取它.$_POST 数组始终为空.这是因为它需要 yval=xval&[rinse_and_repeat] 形式的数据.它没有找到这样的数据,只有 JSON 字符串,它只是把它扔掉了.我检查了请求标头,POST 数据正确发送.

同样,如果我使用 application/json 标头,我将再次无法通过 $_POST 数组访问发送的数据.如果要使用 application/json 内容类型标头,则必须通过 php://input 而不是 $_POST 访问 PHP 中的原始 POST 数据.

The pitfall I ran into:
Initially, I tried to send the JSON string with the header of application/x-www-form-urlencoded and then tried to immediately read it out of the $_POST array in PHP. The $_POST array was always empty. That's because it is expecting data of the form yval=xval&[rinse_and_repeat]. It found no such data, only the JSON string, and it simply threw it away. I examined the request headers, and the POST data was being sent correctly.

Similarly, if I use the application/json header, I again cannot access the sent data via the $_POST array. If you want to use the application/json content-type header, then you must access the raw POST data in PHP, via php://input, not with $_POST.

参考文献:
1) 如何在 PHP 中访问 POST 数据:如何在 PHP 中访问 POST 数据?
2) application/json 类型的详细信息,以及一些可以转换为 JSON 字符串并发送到服务器的示例对象:http://www.ietf.org/rfc/rfc4627.txt

这篇关于将 JSON 数据从 Javascript 发送到 PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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