-1

I have asmx service & when on browser i run url. at that time i am getting xml + json response.

my service code:

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;

        }
    }
}

Now, when i run url on browser. My response is:

<?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>

also i add code on config file.

<httpHandlers>
      <remove verb="*" path="*.asmx"/>
      <add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
    </httpHandlers>

i think. i done everything what required to get json response. also i checked on Internet but they all are same way which i done.

Kindly let me know. what i am making wrong.

Thanks

Kvadiyatar
  • 929
  • 14
  • 29
  • Hi, I got Minus due to Duplicate. but i tried all ways which you suggest on answer. but still not work that's why i ask question. – Kvadiyatar Apr 29 '14 at 05:46
  • 2nd thing i resolve it. by NOT ANSWER WHICH U MENTION ON ANSWER. i must have to post data. nothing else. it was my mistake.& i resolve my self.:D – Kvadiyatar Apr 29 '14 at 05:47

1 Answers1

0

There is no problem with the code. But do you need this to be accessible via browser's address bar or via ajax' script? By default, browser sends Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 in HttpRequest when you open page in window. That's why you see this.

If you use jQuery then depending on 'datatype' parameter you'll get what you want http://api.jquery.com/jQuery.ajax. Something like:

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

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

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

Otherwise see how Accept http header works https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation.

Andrey Borisko
  • 4,511
  • 2
  • 22
  • 31