如何构建一个JSON对象发送到一个AJAX web服务? [英] How do I build a JSON object to send to an AJAX WebService?
问题描述
试图手工格式化我的JSON数据JavaScript和惨遭失败后,我意识到可能有一个更好的办法。下面介绍一下code为Web服务方法和相关类看起来像在C#中:
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得到有效的响应,因为消息我从服务器返回的有我的手工codeD 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.
由于该请求需要HTTP POST我不能使用jQuery的的getJSON
功能,所以我使用的是较低级别的 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
函数在提交规定的一切数据:
,这是我的问题。如何构建在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
我最终会被揪出来的数据在表格文本输入框,但现在硬codeD测试数据是好的。
I'll eventually be pulling data out of text inputs in forms, but for now hard-coded test data is fine.
如何建立正确格式的JSON对象发送到Web服务?
的更新:原来,我的要求的问题是不是JSON的格式,如T.J.指出的那样,而是我的JSON文本不符合Web服务的要求。下面是基于的WebMethod的code有效的JSON请求:的
'{"request":{"Address":{"Address1":"123 Main Street","Address2":"suite 20","City":"New York","State":"NY","Zip":"10000","AddressClassification":null}}}'
这带来了一个问题:<一href=\"http://stackoverflow.com/questions/2738321/when-is-case-sensitivity-important-in-json-requests-to-asp-net-web-services-asmx\">When是区分大小写的JSON请求到ASP.NET Web服务的重要(ASMX)?
推荐答案
答案是很容易的,根据我的previous帖子<一个href=\"http://stackoverflow.com/questions/2670147/can-i-return-json-from-an-asmx-web-service-if-the-contenttype-is-not-json/2671583#2671583\">http://stackoverflow.com/questions/2670147/can-i-return-json-from-an-asmx-web-service-if-the-contenttype-is-not-json/2671583#2671583和<一个href=\"http://stackoverflow.com/questions/2651091/jquery-ajax-call-to-httpget-webmethod-c-not-working/2656543#2656543\">http://stackoverflow.com/questions/2651091/jquery-ajax-call-to-httpget-webmethod-c-not-working/2656543#2656543.
The answer is very easy and based on my previous posts http://stackoverflow.com/questions/2670147/can-i-return-json-from-an-asmx-web-service-if-the-contenttype-is-not-json/2671583#2671583 and http://stackoverflow.com/questions/2651091/jquery-ajax-call-to-httpget-webmethod-c-not-working/2656543#2656543.
该数据应该是JSON-CN codeD。你应该单独连接code每个输入参数。因为你只有一个参数,你应该不喜欢以下内容:
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请求的参数 {要求:$的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);
}
})
而不是$ .toJSON其中来自JSON插件,您可以使用来自 HTTP另一个版本(JSON.stringify)://www.json。组织/
如果您的WebMethod有一些参数,如
If your WebMethod had parameters like
public Response ValidateAddress(Request request1, Request myRequest2)
数据的价值
的 AJAX
调用的参数应该像
data: {request1:$.toJSON(myData1), myRequest2:$.toJSON(myData2)}
或
data: {request1:JSON.stringify(myData1), myRequest2:JSON.stringify(myData2)}
如果您preFER JSON的另一个版本EN codeR。
if you prefer another version of JSON encoder.
这篇关于如何构建一个JSON对象发送到一个AJAX web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!