在母版页上进行 jQuery Ajax 调用?
我错过了什么吗?我正在尝试使用 jquery 对我网站上的 Web 服务进行简单的 ajax 调用,每次从母版页进行调用时都会收到 500 错误。没有人从母版页进行过 ajax 调用,还是我只是疯了并且极度缺乏睡眠?
示例:
MasterPage.master
<script type="text/javascript">
$(document).ready(function () {
$.ajaxSetup({ dataType: "json", contentType: "application/json; charset=utf-8" });
$.ajax({
url: '<%= ResolveUrl("~/Services/Test.asmx/HelloWorld") %>',
success: function (data) {
alert(data);
},
error: function (xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
}
});
});
</script>
/Services/Test.asmx
<WebMethod()> _
Public Function HelloWorld() As String
Return "Hello World"
End Function
看到什么问题了吗?我对母版页有误解吗?请帮忙!
Am I missing something? I'm trying to do a simple ajax call using jquery to a web service on my site and I get a 500 error every time when I make the call from the master page. Has no one ever made an ajax call from a master page or am I just crazy and extremely deprived of sleep?
Example:
MasterPage.master
<script type="text/javascript">
$(document).ready(function () {
$.ajaxSetup({ dataType: "json", contentType: "application/json; charset=utf-8" });
$.ajax({
url: '<%= ResolveUrl("~/Services/Test.asmx/HelloWorld") %>',
success: function (data) {
alert(data);
},
error: function (xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
}
});
});
</script>
/Services/Test.asmx
<WebMethod()> _
Public Function HelloWorld() As String
Return "Hello World"
End Function
See anything wrong? Do I have a misunderstanding of the Master Page? Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我相信我已经解决了这个问题。现在我开始认为我只是困了。我遇到的几个问题,我将向其他人列出它们,以确保它们将来不会这样做:
1) 我记得之前读过另一篇文章,其中解释了通过 jQuery 库进行 ajax 调用确实如此不像数据的空对象,所以即使它是一个空数组也必须列出一些内容。所以,这正是我添加的内容:
2) 一旦解决了 jQuery ajax 问题,我就会收到来自 Web 服务本身的错误消息。警告我,为了能够从脚本调用 Web 服务,我必须添加以下行:
这解决了这个问题,现在我可以在它所在的任何地方调用它。感谢您的帖子,但看起来我只是像往常一样忽略了事情......
Ok, I believe I have figured out the problem. Now I'm beginning to think I am just sleepy. A couple of issues that I had and I'll list them for everyone else to make sure they DON'T do in the future:
1) I remember reading another post previously that explained that an ajax call via the jQuery library does not like a null object for data so something has to be listed even if it's an empty array. So, that's exactly what I added:
2) Once I got past the jQuery ajax problem, I was then presented an error message from the web service itself. Warned me that to be able to call the web service from a script, I have to add the following line:
This solved it and now I can call it wherever it resides. Thanks for the posts but looks like I was just overlooking things as usual...