小编典典

jQuery发布JSON

json

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

你好,同一个老,同一个老… :)

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

我正在使用以下代码来字符串化并在代码区域中显示数据:

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

我想要的是将此数据发送到JSON文件。我一直在从事这个项目:http :
//ridegrab.com/profile_old/,如果您按Submit Query按钮,您将看到页面的顶部。

我也想使用这段脚本来发送数据:

    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!"); 
        }
    })
}

再次,我想要的是能够将JSON数据发送到服务器。我的服务器update or POST在正确的位置设置了数据。


阅读 304

收藏
2020-07-27

共1个答案

小编典典

“数据”应为字符串化的JavaScript对象:

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

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

data: JSON.stringify(formData)

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

contentType: 'application/json'
2020-07-27