jQuery 发布 JSON [英] jQuery posting JSON

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

问题描述

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

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

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

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

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

I have a form called <form id="testForm" action="javascript:test()"> and a code area called <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, '	');
var value = JSON.stringify(formData, null, '	');

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

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 Query button you will see the head of the page populate.

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

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

同样,我想要的只是能够将 JSON 数据发送到服务器.我的服务器设置为在正确的位置更新或 POST 数据.

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

推荐答案

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

'data' should be a stringified JavaScript object:

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

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

To send your formData, pass it to stringify:

data: JSON.stringify(formData)

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

Some servers also require the application/json content type:

contentType: 'application/json'

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

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

这篇关于jQuery 发布 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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