我要去哪里错了,呼吁ASMX Web方法从jQuery的? [英] Where am I going wrong in calling ASMX web methods from jquery?
问题描述
我有一个简单的WebMethod上一个asp.net 2.0应用程序(使用1.0扩展而不是3.5 AJAX扩展)。我试图调用从jQuery的方法,当我这样做是为无数例子表明在互联网上,并在这里的话,我得到一个内部服务器错误消息返回。
I have a simple webmethod on an asp.net 2.0 application (using the 1.0 extensions not the 3.5 ajax extensions). I'm attempting to call the method from jQuery and when I do it as the countless examples show on the Internet and here on SO, I get an Internal Server Error message returned.
下面是我目前的code:
Here's my current code:
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string Select(string login)
{
UserProfile profile = UserProfile.GetUserProfile(login);
return "{ FirstName: '" + profile.FirstName + "', " +
"LastName: '" + profile.LastName + "', " +
"EmailAddress: '" + profile.EmailAddress + "', " +
"PhoneNumber: '" + profile.PhoneNumber + "' }";
}
而现在的jQuery的:
And now the jquery:
$.ajax({
type: "POST",
url: "Services/ProfileService.asmx/Select",
dataType: "json",
data: "{'login':'DOMAIN%5CUSER1'}",
contentType: "application/json; charset=utf-8",
success: function(msg){ alert(msg); },
error: function(xhr){ alert(xhr.statusText);}
});
WebService的装饰与 [ScriptService]
属性为好。如果我注释掉的contentType,更改数据类型设置为文本,更改数据是一个查询字符串(名称=值),我得到返回的XML适当。
The webservice is decorated with the [ScriptService]
attribute as well. If I comment out the contentType, change the dataType to text, and change the data to be a query string (name=value), I get the XML returned appropriately.
我在哪里去了?
更新: 我使用jQuery的V1.3.1和测试均IE6和Firefox 3。我得到一致的结果。
UPDATE: I am using jQuery v1.3.1 and testing in both IE6 and Firefox 3. I'm getting consistent results.
推荐答案
这是我的一部分,一个愚蠢的错误。该问题已得到解决。虽然我包括参考AJAX扩展,我忘了重写的HttpHandler的ASMX服务,以ScriptHandlerFactory类。
This was a stupid mistake on my part. The issue has been resolved. While I included the reference to the AJAX Extensions, I forgot to rewrite the httphandler for ASMX services to the ScriptHandlerFactory class.
添加此解决了问题。
既然我已经得到了一些电子邮件,询问的例子或我如何固定的问题,我写了一篇关于如何做到这一点一应俱全博客文章。
Since I have got some emails inquiring about examples or how I fixed the issue, I wrote a blog post about how to do this soup to nuts.
的http://randomactsofcoding.blogspot.com/2009/03/jquery-json-and-asmx-20-services.html
这篇关于我要去哪里错了,呼吁ASMX Web方法从jQuery的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!