[英]How do I send JSON to Asp.NET Webservice?
I have a javascript object that I am serializing using JSON2 library. 我有一个javascript对象,我正在使用JSON2库进行序列化。 I am then trying to pass this JSON string to a ASP.net webservice.
然后我尝试将此JSON字符串传递给ASP.net Web服务。 I have changed the webmethod to try several different parameter configurations but they all result in a '500 - Internal Server Error'
我已经改变了webmethod来尝试几种不同的参数配置,但它们都会导致'500 - 内部服务器错误'
Can someone give me a clue? 有人能给我一个线索吗?
function postDataToService(data) {
$.ajax({
url: "http://localhost:2686/DataCollectionService.asmx/StoreDataOut",
type: "POST",
contentType: "application/json; charset=utf-8",
data: data,
success: showSuccessNotice,
error: showFailureNotice,
dataType: "json"
});
} //postdatatoservice
function convertDataToJSON(jsObj) {
return JSON.stringify({ list: jsObj });
} //converdatatojson
Web Service: 网络服务:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class DataCollectionService : WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string StoreDataOut(List<string> list)
{
return "Complete";
//model functionality
}
}
I got it.. 我知道了..
Step 1 : I wrapped the JS object in another object that contains a property that matches the parameter name at the web method. 第1步 :我将JS对象包装在另一个对象中,该对象包含与web方法中的参数名称匹配的属性。 Notive the quotes around the
通知周围的报价
return JSON.stringify({'json':jsObj});
Step 2 I then serialized this new 'wrapper' object with JSON.stringify(). 步骤2然后我用JSON.stringify()序列化了这个新的'wrapper'对象。
Step 3 Parameter name at web method matched posted json property name. 步骤3 Web方法的参数名称匹配已发布的json属性名称。 Type is 'object'
类型是'对象'
public string StoreDataOut(object json)
{
}
I used the code you provided and was able to post to the webservice without issues. 我使用了您提供的代码,并且能够毫无问题地发布到Web服务。
Some questions: 一些问题:
convertDataToJSON()
was this a typo? convertDataToJSON()
这是一个错字? If not perform the ajax post like the following: $.ajax({
url: "http://localhost:2686/DataCollectionService.asmx/StoreDataOut",
type: "POST",
contentType: "application/json; charset=utf-8",
data: convertDataToJSON(data),
success: showSuccessNotice,
error: showFailureNotice,
dataType: "json"
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.