jQuery发布JSON [英] jQuery posting JSON

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

问题描述

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

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, '\t');
var value = JSON.stringify(formData, null, '\t');

我想要的是将此数据发送到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数据发送到服务器.我的服务器设置为在正确的位置update or 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.

推荐答案

数据"应为字符串化的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将json发布到网络服务

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

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

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