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

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

问题描述

这可能吗?

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

也许有:应用程序/ JSON

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

否则,我可以使用:

Otherwise I can use:

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.

推荐答案

@mellamokb 你的回答将产生简单的后数据的键/值对使用应用程序/ x-WWW的形式urlen codeD MIME类型

@mellamokb Your answer will generate simple post data key/value pairs using the application/x-www-form-urlencoded mime type

@CIRK

如果您想要发布的JSON,你可以做到这一点。

If you want to post JSON you could do this

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

(不使用jQuery这里)

(not using jQuery here)

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

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

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