从Javascript发送JSON数据到PHP? [英] Send JSON data from Javascript to PHP?
问题描述
我如何从Javascript在浏览器发送JSON数据,到服务器,并让PHP解析它呢?
How can I send JSON data from Javascript in the browser, to a server and have PHP parse it there?
推荐答案
我已经得到了很多的信息在这里,所以我想发布一个解决方案,我发现了。
的问题:获取JSON数据从Javascript的浏览器,到服务器,并让PHP成功地解析
The problem: Getting JSON data from Javascript on the browser, to the server, and having PHP successfully parse it.
环境:的Javascript在浏览器(Firefox)在Windows上。 LAMP服务器作为远程服务器:PHP 5.3.2在Ubuntu
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 XMLHtt prequest对象将数据发送到服务器:
要求=新XMLHtt prequestObject()
request.open(POST,JSON_Handler.php,真)
request.setRequestHeader(内容型,应用/ JSON)
request.send(str_json)
[... code,显示响应...]
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 XMLHttpRequestObject()
request.open("POST", "JSON_Handler.php", true)
request.setRequestHeader("Content-type", "application/json")
request.send(str_json)
[... code to display response ...]
4)在服务器上,PHP code读取JSON字符串:
$ str_json =的file_get_contents('php的://输入');
这种读取原始POST数据。 $ str_json
现在包含从浏览器的确切JSON字符串。
4) On the server, PHP code to read the JSON string:
$str_json = file_get_contents('php://input');
This reads the raw POST data. $str_json
now contains the exact JSON string from the browser.
什么工程(第2版):
1)如果我想使用应用程序/ x-WWW的形式urlen codeD
请求头,我需要建立一个标准的POST字符串X = Y&安培; A = B [等]
,以便当PHP得到它,它可以把它的$ _POST关联数组中开始。因此,在Javascript在浏览器中:
VAR str_json =json_string =+(JSON.stringify(myObject的))
PHP现在可以填充$ _POST阵列时,我通过AJAX / XMLHtt prequest发送str_json如上述第1版。
显示 $的内容_ POST ['json_string']
将显示JSON字符串。用JSON字符串$ _POST数组元素上使用json_de code()将正确地去code的数据,并把它放在一个数组/对象。
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))
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.
我遇到的陷阱:
起初,我试图发送JSON字符串格式application / x-WWW的形式urlen codeD的标题,然后试图立即读出来的$ _POST数组中的PHP。 $ _POST数组总是空的。这是因为它期待的形式利用yval = XVAL和放大器的数据; rinse_and_repeat。它发现没有这样的数据,只有JSON字符串,它只是把它扔了。我检查了请求头,并使用POST数据发送正常。
同样,如果我使用的应用程序/ JSON的头,我又无法通过$ _POST数组访问发送的数据。如果你想使用的应用程序/ JSON Content-Type头,那么你必须访问PHP原始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)如何访问POST数据在PHP中:如何访问POST数据在PHP?
2)上的应用程序/ JSON类型详细信息,与一些可转化为JSON字符串和发送给服务器样品对象:的http ://www.ietf.org/rfc/rfc4627.txt
References:
1) How to access POST data in PHP: How to access POST data in PHP?
2) Details on the application/json type, with some sample objects which can be converted to JSON strings and sent to the server: http://www.ietf.org/rfc/rfc4627.txt
这篇关于从Javascript发送JSON数据到PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!