从 ASMX Web 服务返回 JSON,没有 XML 包装器? [英] Return JSON from ASMX web service, without XML wrapper?

查看:25
本文介绍了从 ASMX Web 服务返回 JSON,没有 XML 包装器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 C# 网络服务中获取 Json 数据.

I need to get Json data from a C# web service.

我知道有几个基于此的问题,相信我,我已经通读了很多,但只会让我更加困惑.

I know there are several questions based on this, trust me I have read through quite a few but only to confuse me further.

这就是我所做的:

在我的网络服务中,我包含了:[System.Web.Script.Services.ScriptService] 用于类 &[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)] 方法

In my web service I have included : [System.Web.Script.Services.ScriptService] for the class & [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)] for the method

我还使用了 JavaScriptSerializer() 将我的数据转换为字符串

I have also used a JavaScriptSerializer() to convert my data to a string

我正在使用 $.getJSON()

如果我不使用它,我会收到一个跨域引用错误.

If I don't use that I get an Cross domain reference error.

为此,我必须设置 m 服务以获取回调函数名称所以我传递 this.Context.Request["callback"] + 序列化的 Json 数据;

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;

但在输出中我把它包裹在

But in the output I get it wrapped in

< string xmlns="http://XYZ...">  

标签内的数据是我需要的格式

The data within the tags is in the format I need

我还尝试使用以下方法设置内容类型:$.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/json; charset=utf-8"});

I also tried setting content type using : $.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/json; charset=utf-8"});

但仍然没有成功.

后来添加:我接受了 frenchie 的 anwser,因为我知道这是正确的方法,但我仍然反刍不能让它工作......我只是把 webservice &同一域中的网站 &使用 xml,我知道这不是最好的方法,但我花了 2 天的时间来研究它&不能再浪费了.

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.

推荐答案

使用这个:

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    
}

然后在服务器端,在您的 AppCode 文件夹中自动生成的代码隐藏文件中,您编写如下内容:

and then on the server side, in the code behind file that's auto-generated in your AppCode folder, you write something like this:

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);
        }
    }

如果您不使用自定义转换器,则 json 字符串和服务器端对象的 c# 类定义之间的属性必须匹配才能进行反序列化.对于序列化,如果您没有自定义转换器,json 字符串将包含您的 c# 类的每个属性.您可以在 c# 类中的属性定义之前添加 [ScriptIgnore],如果您不指定自定义转换器,该属性将被序列化程序忽略.

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.

这篇关于从 ASMX Web 服务返回 JSON,没有 XML 包装器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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