jQuery 发布 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5570747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:23:12  来源:igfitidea点击:

jQuery posting JSON

jqueryjsonpost

提问by Patrioticcow

update: I would like to pass the var valueto the server

更新:我想传递var value给服务器

hello, same old, same old ... :)

你好,老样子,老样子...... :)

I have a form called <form id="testForm" action="javascript:test()">and a code area called <code id="testArea"></code>

我有一个名为的表单<form id="testForm" action="javascript:test()">和一个名为的代码区域<code id="testArea"></code>

I am using this code to stringify and display the data in the code area:

我正在使用此代码在代码区域中对数据进行字符串化和显示:

var formData = form2object('testForm');
document.getElementById('testArea').innerHTML = JSON.stringify(formData, null, '\t');
var value = JSON.stringify(formData, null, '\t');

What I want is to send this data to a JSON file. I've been working on this project : http://ridegrab.com/profile_old/and if you press Submit Querybutton you will see the head of the page populate.

我想要的是将此数据发送到 JSON 文件。我一直在研究这个项目:http: //ridegrab.com/profile_old/,如果你按下Submit Query按钮,你会看到页面的头部填充。

Also I want use this piece of script to send data:

另外我想用这段脚本来发送数据:

    function authenticate(userName, password) {
    $.ajax
    ({
        type: "POST",
        //the url where you want to sent the userName and password to
        url: 'username:password@link to the server/update',
        dataType: 'json',
        async: false,
        //json object to sent to the authentication url
        data: '{"userName": "' + userName + '", "password" : "' + password + '"}',
        success: function () {

        alert("Thanks!"); 
        }
    })
}

Again, all I want is to be able to send that JSON data to the server. My server is set up to update or POSTthe data in the right place.

同样,我想要的只是能够将 JSON 数据发送到服务器。我的服务器设置update or POST为正确位置的数据。

回答by teknopaul

You post JSON like this

你像这样发布JSON

$.ajax(url, {
    data : JSON.stringify(myJSObject),
    contentType : 'application/json',
    type : 'POST',
    ...

if you pass an object as settings.data jQuery will convert it to query parameters and by default send with the data type application/x-www-form-urlencoded; charset=UTF-8, probably not what you want

如果您将对象作为 settings.data 传递,jQuery 会将其转换为查询参数,并默认使用数据类型 application/x-www-form-urlencoded 发送;charset=UTF-8,可能不是你想要的

回答by Kyle Wild

'data' should be a stringified JavaScript object:

'data' 应该是一个字符串化的 JavaScript 对象:

data: JSON.stringify({ "userName": userName, "password" : password })

To send your formData, pass it to stringify:

要发送您的formData,请将其传递给stringify

data: JSON.stringify(formData)

Some servers also require the application/jsoncontent type:

一些服务器还需要application/json内容类型:

contentType: 'application/json'

There's also a more detailed answer to a similar question here: Jquery Ajax Posting json to webservice

这里还有一个类似问题的更详细的答案:Jquery Ajax Posting json to webservice

回答by nizam.sp

In case you are sending this post request to a cross domain, you should check out this link.

如果您将此发布请求发送到跨域,您应该查看此链接。

https://stackoverflow.com/a/1320708/969984

https://stackoverflow.com/a/1320708/969984

Your server is not accepting the cross site post request. So the server configuration needs to be changed to allow cross site requests.

您的服务器不接受跨站点发布请求。因此需要更改服务器配置以允许跨站点请求。