Asp.net 在母版页上调用jQuery Ajax?
我错过什么了吗?我试图使用jquery对我站点上的web服务进行一个简单的ajax调用,每次从母版页调用时,我都会收到500个错误。难道从来没有人从母版页调用过ajax,或者我只是疯了,睡眠极度缺乏 例如: 母版页Asp.net 在母版页上调用jQuery Ajax?,asp.net,jquery,ajax,web-services,master-pages,Asp.net,Jquery,Ajax,Web Services,Master Pages,我错过什么了吗?我试图使用jquery对我站点上的web服务进行一个简单的ajax调用,每次从母版页调用时,我都会收到500个错误。难道从来没有人从母版页调用过ajax,或者我只是疯了,睡眠极度缺乏 例如: 母版页 $(文档).ready(函数(){ $.ajaxSetup({dataType:“json”,contentType:“application/json;charset=utf-8”}); $.ajax({ url:“”, 成功:功能(数据){ 警报(数据); }, 错误:函数(x
$(文档).ready(函数(){
$.ajaxSetup({dataType:“json”,contentType:“application/json;charset=utf-8”});
$.ajax({
url:“”,
成功:功能(数据){
警报(数据);
},
错误:函数(xhr,err){
警报(“readyState:+xhr.readyState+”\n状态:+xhr.status);
警报(“responseText:+xhr.responseText”);
}
});
});
/服务/测试.asmx
_
作为字符串的公共函数HelloWorld()
返回“你好,世界”
端函数
有什么不对劲吗?我对母版页有误解吗?请帮忙 好的,我想我已经解决了问题。现在我开始觉得我只是困了。我有几个问题,我将列出这些问题,供其他人参考,以确保将来不会出现这些问题: 1)我记得之前读过另一篇文章,其中解释了通过jQuery库进行的ajax调用不喜欢数据为空对象,因此即使是空数组,也必须列出一些内容。所以,这正是我添加的内容:
$.ajax({
dataType: "json",
contentType: "application/json; charset=utf-8",
type: 'POST',
data: [],
url: '<%= ResolveUrl("~/Test.asmx/HelloWorld") %>',
success: function (data) {
alert(data);
},
error: function (xhr, err) {
//alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
//alert("responseText: " + xhr.responseText);
$('#ajaxResponse').html(xhr.responseText);
}
});
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Test
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function HelloWorld() As String
Return "[{""Hello World""}]"
End Function
End Class
$.ajax({
数据类型:“json”,
contentType:“应用程序/json;字符集=utf-8”,
键入:“POST”,
数据:[],
url:“”,
成功:功能(数据){
警报(数据);
},
错误:函数(xhr,err){
//警报(“readyState:+xhr.readyState+”\n状态:+xhr.status);
//警报(“responseText:+xhr.responseText”);
$('#ajaxsresponse').html(xhr.responseText);
}
});
2)一旦我解决了jqueryajax问题,就会收到来自web服务本身的错误消息。警告我,为了能够从脚本调用web服务,我必须添加以下行:
$.ajax({
dataType: "json",
contentType: "application/json; charset=utf-8",
type: 'POST',
data: [],
url: '<%= ResolveUrl("~/Test.asmx/HelloWorld") %>',
success: function (data) {
alert(data);
},
error: function (xhr, err) {
//alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
//alert("responseText: " + xhr.responseText);
$('#ajaxResponse').html(xhr.responseText);
}
});
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Test
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function HelloWorld() As String
Return "[{""Hello World""}]"
End Function
End Class
_
_
_
_
公开课考试
继承System.Web.Services.WebService
_
作为字符串的公共函数HelloWorld()
返回“[{”“Hello World”“}]”
端函数
末级
这解决了它,现在我可以把它称为任何地方。感谢您的帖子,但看起来我只是像往常一样忽略了一些事情…没有任何特定于母版页的内容会导致这种情况。你是说当你的代码在一个查看页面而不是母版页上时,它会工作吗?当你在浏览器中加载
~/Services/Test.asmx/HelloWorld
时,你是否也会收到500个错误?试着检查IIS日志等。我可以“浏览”到我的Web服务页面(~/Services/Test.asmx/HelloWorld),并且它可以正常加载。这个方法是可用的,我可以调用它。即将验证我是否能够在内容视图页上查看web服务。无法识别意外以“/HelloWorld”结尾的URL的请求格式。描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。异常详细信息:System.InvalidOperationException:无法识别意外以“/HelloWorld”结尾的URL的请求格式。源错误:[InvalidOperationException:无法识别意外以“/HelloWorld”结尾的URL的请求格式。]