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

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

问题描述

我需要从C#Web服务获取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服务以获取回调函数名称 所以我要传递t his.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"});

但仍然没有成功.

稍后再添加:因为我知道这是正确的方法,所以我接受了Frenchie的答案,但是我仍然无法使它正常工作...我只是将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天全站免登陆