从 ASMX Web 服务返回 JSON,没有 XML 包装器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8405458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return JSON from ASMX web service, without XML wrapper?
提问by Praneeta
I need to get Json data from a C# web service.
我需要从 C# Web 服务中获取 Json 数据。
I know there are several questions based on this, trust me I have read through quite a few but only to confuse me further.
我知道有几个基于此的问题,相信我,我已经阅读了很多,但只会让我更加困惑。
This is what I have done :
这就是我所做的:
In my web service I have included : [System.Web.Script.Services.ScriptService]for the class & [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]for the method
在我的 Web 服务中,我包含了:[System.Web.Script.Services.ScriptService]用于类和[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]用于方法
I have also used a JavaScriptSerializer()to convert my data to a string
我还使用 aJavaScriptSerializer()将我的数据转换为字符串
I am calling this service using $.getJSON()
我正在使用 $.getJSON()
If I don't use that I get an Cross domain reference error.
如果我不使用它,我会收到一个跨域引用错误。
To do this I had to setup m service to get the callback function name
so I am passing this.Context.Request["callback"]+ serialized Json Data;
为此,我必须设置 m 服务以获取回调函数名称,因此我要传递 t his.Context.Request["callback"]+ 序列化的 Json 数据;
But in the output I get it wrapped in
但是在输出中我得到了它
< string xmlns="http://XYZ...">
The data within the tags is in the format I need
标签中的数据采用我需要的格式
I also tried setting content type using : $.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/json; charset=utf-8"});
我还尝试使用以下方法设置内容类型: $.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/json; charset=utf-8"});
But still no success.
但仍然没有成功。
Addded later: I accepted frenchie's anwser beacuse I know it is the correct approach but I stil cud not get it to work... I just put the webservice & website in the same domain & used xml, I know it wasnt the best way, but I had spent 2 days on it & could not afford to waste more.
后来补充:我接受了法国人的 anwser 因为我知道这是正确的方法,但我仍然拒绝让它工作......我只是将网络服务和网站放在同一个域中并使用了 xml,我知道这不是最好的方法,但我已经花了 2 天时间,不能再浪费了。
采纳答案by frenchie
Use this:
用这个:
var JsonString = ....;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "YourWebServiceName.asmx/yourmethodname",
data: "{'TheData':'" + JsonString + "'}",
dataType: "json",
success: function (msg) {
var data = msg.hasOwnProperty("d") ? msg.d : msg;
OnSucessCallBack(data);
},
error: function (xhr, status, error) {
alert(xhr.statusText);
}
});
function OnSuccessCallData(DataFromServer) {
// your handler for success
}
and then on the server side, in the code behind file that's auto-generated in your AppCode folder, you write something like this:
然后在服务器端,在 AppCode 文件夹中自动生成的代码隐藏文件中,您编写如下内容:
using System.Web.Services;
using System.Web.Script.Serialization;
[System.Web.Script.Services.ScriptService]
public class YourWebServiceName : System.Web.Services.WebService
{
[WebMethod]
public string yourmethodname(string TheData)
{
JavascriptSerializer YourSerializer = new JavascriptSerializer();
// custom serializer if you need one
YourSerializer.RegisterConverters(new JavascriptConverter [] { new YourCustomConverter() });
//deserialization
TheData.Deserialize(TheData);
//serialization
TheData.Serialize(TheData);
}
}
If you don't use a custom converter, the properties between the json string and the c# class definition of your server-side object must match for the deserialization to work. For the serialization, if you don't have a custom converter, the json string will include every property of your c# class. You can add [ScriptIgnore]just before a property definition in your c# class and that property will be ignored by the serializer if you don't specify a custom converter.
如果不使用自定义转换器,则 json 字符串和服务器端对象的 c# 类定义之间的属性必须匹配,反序列化才能工作。对于序列化,如果您没有自定义转换器,json 字符串将包含您的 c# 类的每个属性。您可以[ScriptIgnore]在 c# 类中的属性定义之前添加,如果您未指定自定义转换器,则序列化程序将忽略该属性。

