.NET Webservice still returning XML instead of Json

I was trying to call a webservice method through the jquery ajax metod and guess what: I was always getting an xml response

$.ajax(
{
type: “POST”,
url: http://localhost:50080/ElearningWebApplication/ChatWebService.asmx/InserMessage,
data: “{}”,
contentType: “application/json; charset=utf-8”,
dataType: “json”,
crossDomain: true,
success: function (response) {
//email was sent
alert(“Email has been sent successfully!”);
},
error: function (jqXHR, error, errorThrown) {
if (jqXHR.status && jqXHR.status == 400) {
alert(jqXHR.responseText);
} else {
alert(“Something went wrong. We could not send your email.”);
}
}

}

);

 I included the correct attributes to both the service and the method

[WebService(Namespace = http://www.action.gr/”)]
[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 ChatWebService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

          public string InsertMessage(string text, int roomID, int userID, string color, int toUserID)
{
…..
}
}

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.webServer>
        <handlers>
            <add name="ScriptHandlerFactory"
                 verb="*" path="*.asmx"
                 type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                 resourceType="Unspecified" />
        </handlers>
    </system.webServer>
</configuration>

Final Checklist

  1. Did you specify type: “POST” in the ajax request? A security feature of ASP.NET web services that are JSON serialized through the ASP.NET AJAX extensions is that they must be requested in a specific way. This is an important deterrent against your services being used in XSS attacks.
  2. Did you specify contentType: “application/json; charset=utf-8” in the ajax request?
  3. Did you specify dataType: “json”in the ajax request?
  4. Does your .asmx web service include the [ScriptService]` attribute?
  5. Does your web method include the [ScriptMethod(ResponseFormat = ResponseFormat.Json)] attribute? (My code works even without this attribute, but a lot of articles say that it is required)
  6. Have you added the ScriptHandlerFactory to the web.config file in ?
  7. Have you removed all handlers from the the web.config file in in ?
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s