views:

53

answers:

3

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:




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</string>
+1  A: 

I think this has already been answered here.

iaimtomisbehave
+1  A: 

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>
Peter Conrey
A: 

I face with the same problem. I add these lines to web.config and my web application can not run. I am using VS2005. I know that VS2008 with .Net 3.5 do not have problem. But we have to using VS2005. Please help.

QuangND