从 jQuery 调用 ASMX WebMethod - 响应文本为空 xhr.status = “error”
我完全困惑了。
- jquery v1.5.2 Firefox 3.6.16 ASMX
- 列表项
- Web 服务用 VS 2010、.Net Framework 3.5 编写
- 托管在本地计算机的 VS 2010 开发 Web 服务器上或在
- Windows Web Server 2008 R2 上运行 IIS 7.5 的主 Web 服务器
两个本地开发 Web 服务器和主要生产 Web 服务器表现出相同的行为
- 。从浏览器调用时,Web 服务运行良好。
- 我得到了方法列表。
- 我可以单击方法名称
- 我可以单击 Invoke 按钮,调用该方法并返回结果
当我的 jquery 页面调用相同的 Web 方法时,将触发错误函数,并显示以下状态字段
readState : 0 响应文本:“” 状态:0 状态文本:“错误” 错误:function()
我的 jQuery 部分
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: 'POST',
url: 'http://localhost:1272/ndtvservices.asmx/HelloWorld',
data: '{}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(msg) {
alert("success " + msg) ;
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message) ;
}
});
});
</script>
我的 web 方法
导入 System.Web.Services
导入 System.Web.Services.Protocols
导入 System.ComponentModel
导入 System.Web.Script。服务
导入 System.Web.Script.Serialization
' 要允许使用 ASP.NET AJAX 从脚本调用此 Web 服务,请取消注释以下行。 '
_
_ _
公共课服务1 继承 System.Web.Services.WebService
'<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=False, XmlSerializeString:=False)> _
<WebMethod()> _
Public Function HelloWorld() As String
Dim js As New JavaScriptSerializer
Dim s As String = "Hello World"
Dim sReturn As String = js.Serialize(s)
Return sReturn
End Function
结束类
我在 system.web 下的 Web 配置有这些条目,正如我在某处读到的那样
<webServices>
<protocols>
<add name="HttpGet"></add>
<add name="HttpPost"></add>
</protocols>
</webServices>
_
我尝试过对上述语句进行注释和取消注释。
据我了解,调用此方法的非 asp.net ajax 页面不需要这样做。
我做错了什么?
非常感谢,
祝
艾耶
Am completely flummoxed.
- jquery v1.5.2 Firefox 3.6.16 ASMX
- List item
- Web service written in VS 2010, .Net Framework 3.5
- Hosted on local computer's VS 2010 Development web server OR ON
- Main web server running IIS 7.5 on Windows Web Server 2008 R2
Both local dev web server and the main production web server exhibit same behavior
- The web service works well when invoked from the browser.
- I get the list of methods.
- I can click on the method name
- I can click the Invoke button and the method is invoked and the result is returned
When my jquery page calls the same web method, the error function is fired with the following status fields
readyState : 0
responseText : ""
status : 0
statusText : "error"
error : function()
My jQuery portion
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: 'POST',
url: 'http://localhost:1272/ndtvservices.asmx/HelloWorld',
data: '{}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(msg) {
alert("success " + msg) ;
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message) ;
}
});
});
</script>
My web method
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Web.Script.Services
Imports System.Web.Script.Serialization
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' _
_
_
_
Public Class Service1
Inherits System.Web.Services.WebService
'<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=False, XmlSerializeString:=False)> _
<WebMethod()> _
Public Function HelloWorld() As String
Dim js As New JavaScriptSerializer
Dim s As String = "Hello World"
Dim sReturn As String = js.Serialize(s)
Return sReturn
End Function
End Class
My web config under system.web has these entries as I read on SO somewhere
<webServices>
<protocols>
<add name="HttpGet"></add>
<add name="HttpPost"></add>
</protocols>
</webServices>
_
I have tried with the above statement commented and uncommented.
As I understand this, this should not be required for a non asp.net ajax page calling this method.
What am I doing wrong?
Thanks a bunch
Best wishes
Iyer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该在服务方法中手动 JSON 序列化响应。 ASP.NET 已经自动为您完成这项工作。您所需要的只是:
并确保您的服务使用 ScriptService 属性进行装饰(我的 VB.NET 非常生锈,因此这可能会有点偏差):
另外,我会避免在 $.ajax() 中使用完全限定的 URI的 url 参数。这应该就是您所需要的:
最后,如果您使用的是 ASP.NET 3.5 或更高版本,ASP.NET 将 JSON 响应包装在“.d”对象中,以防止与 JSON 数组相关的特定安全问题。您需要在成功回调中“点”到该包装对象中,如下所示:
You should not be manually JSON serializing the response in your service method. ASP.NET already does that for you automatically. All you need is:
And ensure that your service is decorated with the ScriptService attribute (my VB.NET is pretty rusty, so this might be off a little):
Also, I would avoid using the fully qualified URI in $.ajax()'s url parameter. This should be all you need:
Finally, if you're using ASP.NET 3.5 or later, ASP.NET wraps the JSON response in a ".d" object to protect against a particular security issue concerning JSON arrays. You'll want to "dot" into that wrapper object in your success callback like this: