Javascript:使用 Ajax 发送 JSON 对象? [英] Javascript : Send JSON Object with Ajax?

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

问题描述

这可能吗?

xmlHttp.send({
    "test" : "1",
    "test2" : "2",
});

也许:一个带有 content type 的标题:application/json?:

Maybe with: a header with content type : application/json?:

xmlHttp.setRequestHeader('Content-Type', 'application/json')

否则我可以使用:

xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')

然后 JSON.stringify JSON 对象并将其作为参数发送,但如果可能的话,以这种方式发送它会很酷.

and then JSON.stringify the JSON object and send it in a parameter, but it would be cool to send it in this way if it's possible.

推荐答案

使用 jQuery:

$.post("test.php", { json_string:JSON.stringify({name:"John", time:"2pm"}) });

没有 jQuery:

var xmlhttp = new XMLHttpRequest();   // new HttpRequest instance 
xmlhttp.open("POST", "/json-handler");
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.send(JSON.stringify({name:"John Rambo", time:"2pm"}));

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

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