【问题标题】:Asmx service return json data instead of xml [duplicate]Asmx服务返回json数据而不是xml [重复]
【发布时间】:2014-04-26 12:22:29
【问题描述】:

我有 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 响应所需的一切。我也在网上查了一下,但它们都和我做的一样。

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

谢谢

【问题讨论】:

  • 嗨,由于重复,我得到了减号。但我尝试了你在回答时建议的所有方法。但仍然不行,这就是我问问题的原因。
  • 第二件事我解决了。 by NOT ANSWER U MENTION ON ANSWER。我必须发布数据。没有其他的。这是我的错误。&我解决了我的问题。:D

标签: c# json asmx


【解决方案1】:

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

如果你使用 jQuery,那么根据 'datatype' 参数你会得到你想要的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);});

否则请查看 Accept http 标头如何工作https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation

【讨论】:

    猜你喜欢
    • 2020-06-02
    • 2012-06-20
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    相关资源
    最近更新 更多