var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
alert("Data Loaded: " + JSON.parse(xmlhttp.responseText).name);
}
}
xmlhttp.send(JSON.stringify({name:"wqf", time:"2pm"}));
JSON.parse(text):解析一个字符串到JSON Object
JSON.stringify():转换一个JSON Object到一个字符串
本文介绍了一种使用XMLHttpRequest对象进行POST请求的方法,并通过设置请求头来发送JSON格式的数据。此外,还演示了如何使用JSON.parse()和JSON.stringify()进行数据格式的转换。

56

被折叠的 条评论
为什么被折叠?



