سؤال

My ASMX call is returning Content-Type = text/xml; charset=utf-8

I am using ASP.NET 3.5 and jQuery.Ajax.
Have added these things are per numerous suggestions from Stack Overflow

I have done these things

  1. Done this at web.config

    <remove verb="*" path="*.asmx"/>
    <add verb="*" path="*.asmx" validate="false"
    type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    
  2. On the function call inside ASMX, I am using these

    <WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function GetCompanies(SearchedCompany As String) As String
    
  3. the jQuery Ajax call is like this

    var searchString = "SearchedCompany=" + $(".auto-company").val();
    $.ajax({
        type: "POST",
        url: "Services/CompaniesService.asmx/GetCompanies",
        data: searchString,
        dataType: 'json',
        success: function (msg) {
            companies = msg.hasOwnProperty("d") ? msg.d : msg;
        },
        error: function (xhr, status, error) {
    
        }
    });
    

But whatever I do, the response header is still of type xml. What part am I missing here?

Disclaimer: I know this has been asked many times around here. But was not able to find any answer. As you can see, I have already implemented all of the solutions provided.

هل كانت مفيدة؟

المحلول

You need to set the contentType as well to "application/json; charset=utf-8"

http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/

Because jQuery does set the content-type on POST requests that have data, the content-type gets set to the default “application/x-www-form-urlencoded” initially. Then in IE7, setRequestHeader was adding “application/json; charset=utf-8″ to that instead of truly setting it. So, the content-type was incorrect for JSON serialization and the web service was returning XML instead of JSON.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top