[英]Send JSon data from javascript to php
我正在尝试使用JSon将javascript中的数组发布到php会话变量。 我搜索了很多东西,有3种发布方式; HTML表单发布请求,AJAX和cookie。 我想通过发布请求来发布,在脚本中我将数组转换为JSon对象没有问题。 然后我试图像这样将其发送到php页面;
$('#firstConfirmButton').click(function(){
var json_text = JSON.stringify(ordersArray, null);
alert(json_text.toString());
request= new XMLHttpRequestObject();
request.open("POST", "test.php", true);
request.setRequestHeader("Content-type", "application/json");
request.send(json_text);
});
在test.php中,我尝试通过使用file_get_contents('php:// input')获取数组,但是它不起作用。 当我尝试回显son_text时,它显示的是“ firstConfirmButton =”而不是数组内容。 我该怎么办?
<?php
require("includes/db.php");
require("includes/functions.php");
session_start();
if (isset($_POST['firstConfirmButton'])) {
$json_text = \file_get_contents('php://input');
echo $json_text;
}
?>
在使用jQuery时,请使用jQuery的ajax函数在浏览器上实现更好的兼容性:
$('#firstConfirmButton').click(function(){
$.post("test.php", { data: ordersArray }, function(response){
// success callback
// response variable can be used for response from PHP, if any
});
});
在您的PHP代码中:
print_r($_POST['data']);
请注意, ordersArray
应该是有效的JSON。 然后,您可以根据需要在PHP中使用变量。
我感谢Apul的回答,但您错过了引起问题的内容,请执行以下操作:
$('#firstConfirmButton').click(function(){
var json_text = JSON.stringify(ordersArray, null);
alert(json_text.toString());
var request;
var XMLHttpFactories = [
function () {return new XMLHttpRequest()},
function () {return new ActiveXObject("Msxml2.XMLHTTP")},
function () {return new ActiveXObject("Msxml3.XMLHTTP")},
function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];
function createXMLHTTPObject() {
var xmlhttp = false;
for (var i=0;i<XMLHttpFactories.length;i++) {
try {
xmlhttp = XMLHttpFactories[i]();
}
catch (e) {
continue;
}
break;
}
return xmlhttp;
}
function sendRequest(url,callback,postData) {
var req = createXMLHTTPObject();
if (!req) return;
var method = (postData) ? "POST" : "GET";
req.open(method,url,true);
req.setRequestHeader('User-Agent','XMLHTTP/1.0');
if (postData)
req.setRequestHeader('Content-type','text/json');
req.onreadystatechange = function () {
if (req.readyState != 4) return;
if (req.status != 200 && req.status != 304) {
alert('HTTP error ' + req.status);
return;
}
callback(req);
}
if (req.readyState == 4) return;
req.send(postData);
}
});
最后像这样调用sendRequest
函数:
sendRequest('file.txt',handleRequest,ordersArray);
function handleRequest(req) {
var writeroot = [some element];
writeroot.innerHTML = req.responseText;
}
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","demo_post2.asp",true);
xmlhttp.setRequestHeader("Content-type","text/json");
xmlhttp.send(ordersArray);
$json_text = file_get_contents('php://input');
echo json_decode($json_text); //I have changed the send data from `json_text` to the direct non-Stringified json object, the `ordersArray`.
两者之间的区别在于可伸缩性。
注意:我和Apul的答案之间的区别在于jQuery,但我没有使用它,他使用过。
Apul关于使用AJAX库是正确的。 在PHP方面,我使用“接收”数据
$value = urldecode(file_get_contents('php://input'));
实际上,我仍在尝试解决此问题,并且我认为我已经取得了一些进展。 也许其他人也会遇到同样的问题,希望对他有帮助。 首先,我将JavaScript代码更改为该代码,尤其是添加“ async:false”挽救了我的性命,因为如果没有此更新,Safari和Firefoc无法将任何Json变量发布到php(Chrome可以)。 我花了两天时间来学习这个。
$('#firstConfirmButton').click(function(){
var my_json_val = JSON.stringify(ordersArray, null);
alert(my_json_val.toString());
$.ajax({
type: "POST",
url: "gazanfer.php",
data: my_json_val,
cache: false,
contentType: false,
async: false,
processData: false,
success: function(data){
//alert("---"+data);
alert("Settings has been updated successfully.");
window.location.reload(true);
}
});
});
现在发布可以,但是我仍然无法读取php端的数据。 我不知道为什么,这是我的PHP代码。 有什么主意吗
<?php
require("includes/db.php");
require("includes/functions.php");
$my_json_encoded = $_POST['data'];
$my_json_val = json_decode($my_json_encoded);
echo $my_json_val;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.