Pat*_*cow 178 jquery post 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');
Run Code Online (Sandbox Code Playgroud)
我想要的是将此数据发送到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!");
}
})
}
Run Code Online (Sandbox Code Playgroud)
同样,我想要的是能够将JSON数据发送到服务器.我的服务器设置update or POST为正确的数据.
tek*_*aul 263
你发布这样的JSON
$.ajax(url, {
data : JSON.stringify(myJSObject),
contentType : 'application/json',
type : 'POST',
...
Run Code Online (Sandbox Code Playgroud)
如果你传递一个对象作为settings.data jQuery将它转换为查询参数,默认情况下发送数据类型application/x-www-form-urlencoded; charset = UTF-8,可能不是你想要的
Kyl*_*ild 202
'data'应该是一个字符串化的JavaScript对象:
data: JSON.stringify({ "userName": userName, "password" : password })
Run Code Online (Sandbox Code Playgroud)
要发送,请将其formData传递给stringify:
data: JSON.stringify(formData)
Run Code Online (Sandbox Code Playgroud)
某些服务器还需要application/json内容类型:
contentType: 'application/json'
Run Code Online (Sandbox Code Playgroud)
这里还有一个类似问题的更详细的答案:Jquery Ajax将json发布到webservice
| 归档时间: |
|
| 查看次数: |
352882 次 |
| 最近记录: |