【发布时间】:2013-09-06 00:56:53
【问题描述】:
我正在拼命尝试使用 ajax post 方法将 json 对象传递给 php 文件,对其进行解码并传回一些东西。 php的json_last_error显示4,表示语法错误。
this.send = function()
{
var json = {"name" : "Darth Vader"};
xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","php/config.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("data="+json);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("result").innerHTML=xmlhttp.responseText;
};
};
};
<?php
if(isset($_POST["data"]))
{
$data = $_POST["data"];
$res = json_decode($data, true);
echo $data["name"];
}
?>
【问题讨论】:
-
你为什么不使用jQuery?
-
我对此完全陌生,我不想使用我不了解的框架重新开始
标签: php javascript ajax json