如何构建要发送到 AJAX WebService 的 JSON 对象? [英] How do I build a JSON object to send to an AJAX WebService?

查看:23
本文介绍了如何构建要发送到 AJAX WebService 的 JSON 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试在 javascript 中手动格式化我的 JSON 数据并惨遭失败后,我意识到可能有更好的方法.以下是 C# 中 Web 服务方法和相关类的代码:

After trying to format my JSON data by hand in javascript and failing miserably, I realized there's probably a better way. Here's what the code for the web service method and relevant classes looks like in C#:

[WebMethod]
public Response ValidateAddress(Request request)
{
    return new test_AddressValidation().GenerateResponse(
        test_AddressValidation.ResponseType.Ambiguous);
}

...

public class Request
{
    public Address Address;
}

public class Address
{
    public string Address1;
    public string Address2;
    public string City;
    public string State;
    public string Zip;
    public AddressClassification AddressClassification;
}

public class AddressClassification
{
    public int Code;
    public string Description;
}

Web 服务在使用 SOAP/XML 时效果很好,但我似乎无法使用 javascript 和 jQuery 获得有效响应,因为我从服务器返回的消息与我的手动编码 JSON 有问题.

The web service works great with using SOAP/XML, but I can't seem to get a valid response using javascript and jQuery because the message I get back from the server has a problem with my hand-coded JSON.

我不能使用 jQuery getJSON 函数,因为请求需要 HTTP POST,所以我使用低级 ajax 函数代替:

I can't use the jQuery getJSON function because the request requires HTTP POST, so I'm using the lower-level ajax function instead:

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "http://bmccorm-xp/HBUpsAddressValidation/AddressValidation.asmx/ValidateAddress",
    data: "{"Address":{"Address1":"123 Main Street","Address2":null,"City":"New York","State":"NY","Zip":"10000","AddressClassification":null}}",
    dataType: "json",
    success: function(response){
        alert(response);
    }
})

ajax 函数正在提交 data: 中指定的所有内容,这就是我的问题所在.如何在 javascript 中构建格式正确的 JSON 对象,以便我可以将其插入到我的 ajax 调用中,如下所示:

The ajax function is submitting everything specified in data:, which is where my problem is. How do I build a properly formatted JSON object in javascript so I can plug it in to my ajax call like so:

data: theRequest

我最终会从表单中的文本输入中提取数据,但目前硬编码的测试数据很好.

I'll eventually be pulling data out of text inputs in forms, but for now hard-coded test data is fine.

如何构建格式正确的 JSON 对象以发送到网络服务?

更新:事实证明,我的请求的问题不在于 JSON 的格式,正如 T.J.指出,而是我的 JSON 文本不符合 Web 服务的要求.这是基于 WebMethod 中的代码的有效 JSON 请求:

'{"request":{"Address":{"Address1":"123 Main Street","Address2":"suite 20","City":"New York","State":"NY","Zip":"10000","AddressClassification":null}}}'

这带来了另一个问题:在对 ASP.NET Web 服务 (ASMX) 的 JSON 请求中区分大小写什么时候很重要?

推荐答案

答案很简单,基于我之前的帖子 如果 ContentType 不是 JSON,我可以从 .asmx Web 服务返回 JSON 吗?JQuery ajax 调用 httpget webmethod (c#) 不工作.

The answer is very easy and based on my previous posts Can I return JSON from an .asmx Web Service if the ContentType is not JSON? and JQuery ajax call to httpget webmethod (c#) not working.

数据应该是 JSON 编码的.您应该对每个输入参数进行单独编码.因为您只有一个参数,所以您应该执行以下操作:

The data should be JSON-encoded. You should separate encode every input parameter. Because you have only one parameter you should do like following:

首先将您的数据构建为原生 JavaScript 数据,例如:

first construct you data as native JavaScript data like:

var myData = {Address: {Address1:"address data 1",
                        Address2:"address data 2",
                        City: "Bonn",
                        State: "NRW",
                        Zip: "53353",
                        {Code: 123,
                         Description: "bla bla"}}};

然后作为ajax请求的参数给出{request:$.toJSON(myData)}

then give as a parameter of ajax request {request:$.toJSON(myData)}

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "http://bmccorm-xp/HBUpsAddressValidation/AddressValidation.asmx/ValidateAddress",
    data: {request:$.toJSON(myData)},
    dataType: "json",
    success: function(response){
        alert(response);
    }
})

您可以使用来自 http 的另一个版本 (JSON.stringify),而不是来自 JSON 插件的 $.toJSON://www.json.org/

如果您的 WebMethod 具有类似的参数

If your WebMethod had parameters like

public Response ValidateAddress(Request request1, Request myRequest2)

ajax调用的data参数的值应该是这样的

the value of data parameter of the ajax call should be like

data: {request1:$.toJSON(myData1), myRequest2:$.toJSON(myData2)}

data: {request1:JSON.stringify(myData1), myRequest2:JSON.stringify(myData2)}

如果您更喜欢其他版本的 JSON 编码器.

if you prefer another version of JSON encoder.

这篇关于如何构建要发送到 AJAX WebService 的 JSON 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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