开发者

Problem with ASP.NET Web Service and JQuery Client

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>


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜