2

Am I missing something? I am trying to create a web service and consumer in asp.net, using JSON with JQuery, but I'm not having any luck. I can get JQuery to call the service, and get the service to reply, but the response always goes through the "error" callback in JQuery. When I view the response in FireBug, it appears to be XML, not JSON. Below is my service and the relevant JQuery from the client. Any help would be appreciated:

<%@ WebService Language="C#" Class="ajaxService" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService()]
public class ajaxService: System.Web.Services.WebService {

 [WebMethod()]
 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
 public string HelloWorld() {
  return "Hello World";
 }
}

JQuery

$(document).ready(function() {
  $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: "{}",
    url: "ajaxService.asmx/HelloWorld",
    success: function(msg) {
      alert("success " + msg.d);
    },
    error: function(err) {
      alert(err.status + " : " + err.statusText);
    }
  });
});

The response always states "OK : 200", and the response content is:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/"&gt;Hello World&lt;/string>
1
  • I checked and re-checked how I am doing it and there is no difference between what you are doing and my code that is working. Commented Jan 30, 2010 at 0:59

1 Answer 1

1

Actually, I found the answer here: http://forums.asp.net/p/1054378/2338982.aspx#2338982

Not sure where this guy found the answer, but you have to add this to the web.config:

<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
</httpHandlers>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.