Asmx服务返回json数据而不是xml

时间:2014-04-26 12:22:30

标签: c# json asmx

我有asmx服务&在浏览器上我运行url。那时我得到了xml + json的响应。

我的服务代码:

namespace CTSWebApp.Service
{
    /// <summary>
    /// Summary description for GetShipperConsignee
    /// </summary>
    [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 GetShipperConsignee : System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public string GetAutoList(string SearchText)
        {
            List<ShipperDTO> shipperList = new List<ShipperDTO>();
            ShipperApp shipperApp = new ShipperApp();
            shipperList = shipperApp.GetShipper(SearchText);

            var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string m_json =  serializer.Serialize(shipperList);

            return  m_json;

        }
    }
}

现在,当我在浏览器上运行url时。我的回答是:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">List :[{"VendorId":18,"Name":"GRIP RITE","Address1":"530B NE 42ND COURT","Address2":"","State":"FL","City":"FT LAUDERDALE","Zip":"33334"},{"VendorId":1003895,"Name":"GRIP RITE","Address1":"214-A BICKLEY RD","Address2":"","State":"SC","City":"LEXINGTON","Zip":"29072"},{"VendorId":1009453,"Name":"GRIFFIN PLATING CO","Address1":"1636 WEST ARMITAGE AVE.","Address2":"","State":"IL","City":"CHICAGO","Zip":"60622"},{"VendorId":1012716,"Name":"GRIFFIN TRANSPORT SERVICE","Address1":"5360 CAPITAL COURT","Address2":"STE 100","State":"NV","City":"RENO","Zip":"89502"},{"VendorId":1016190,"Name":"GRIFFIN GREENHOUSE","Address1":"7141 OLD RUTLEDGE PIKE","Address2":"","State":"TN","City":"KNOXVILLE","Zip":"37924"},{"VendorId":1016668,"Name":"GRIOT\u0027S GARAGE INC","Address1":"3333 S 38TH STREET","Address2":"","State":"WA","City":"TACOMA","Zip":"98409"},{"VendorId":1017354,"Name":"GRIFFIN EXPRESS","Address1":"12 CRESCENT STREET","Address2":"","State":"MA","City":"HOLYOKE","Zip":"01040"},{"VendorId":1018691,"Name":"GRIFFIN INDUSTRIES","Address1":"1001 ORIENT ROAD","Address2":"","State":"FL","City":"TAMPA","Zip":"33619"},{"VendorId":1022522,"Name":"GRIND LAP SERVICES","Address1":"1045 WEST NATIONAL","Address2":"","State":"IL","City":"ADDISON","Zip":"60101"},{"VendorId":1022552,"Name":"GRISWOLD CORPORATION","Address1":"ONE RIVER STREET","Address2":"","State":"CT","City":"MOOSUP","Zip":"06354"},{"VendorId":1027089,"Name":"GRIGNARD COMPANY","Address1":"505 CAPOBIANCO PLAZA BLDG","Address2":"","State":"NJ","City":"RAHWAY","Zip":"07065"}]</string>

我也在配置文件中添加代码。

<httpHandlers>
      <remove verb="*" path="*.asmx"/>
      <add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
    </httpHandlers>
我认为。我完成了获得json响应所需的一切。我也在网上查了一下,但他们都和我做的一样。

请告诉我。我犯了什么错。

由于

1 个答案:

答案 0 :(得分:0)

代码没有问题。但是你需要通过浏览器的地址栏或通过ajax&#39;来访问它吗?脚本?默认情况下,当您在窗口中打开页面时,浏览器会在HttpRequest中发送Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8。这就是你看到这个的原因。

如果您使用jQuery,那么取决于&#39;数据类型&#39;参数你将得到你想要的http://api.jquery.com/jQuery.ajax。类似的东西:

        $.ajax({
                 type: "get",
                 url: "Sample.asmx/HelloWorld",

                 contentType: "application/json; charset=utf-8",
                 dataType: "json"

             }).done(function(msg){alert(msg.d);});

否则,请参阅接受 http标头的工作原理https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation