Javascript 无法从母版页调用代码页中的webmethod
我在c#asp.net中有一个webmethod和一个jquery代码来调用该方法。如果我像myservice.asmx一样将代码放在webService页面中,它就可以正常工作。我从同一个母版页调用它。但是当我从.aspx页面调用它时,它根本不起作用。我在按钮点击事件中调用此Web服务。在这两种情况下都会触发按钮单击,我通过javascript中的警报进行了检查 调用webmethod的母版页代码如下:Javascript 无法从母版页调用代码页中的webmethod,javascript,c#,jquery,asp.net,web-services,Javascript,C#,Jquery,Asp.net,Web Services,我在c#asp.net中有一个webmethod和一个jquery代码来调用该方法。如果我像myservice.asmx一样将代码放在webService页面中,它就可以正常工作。我从同一个母版页调用它。但是当我从.aspx页面调用它时,它根本不起作用。我在按钮点击事件中调用此Web服务。在这两种情况下都会触发按钮单击,我通过javascript中的警报进行了检查 调用webmethod的母版页代码如下: <%-- WebService Data--%> <script
<%-- WebService Data--%>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type ="text/javascript" >
$(document).ready(function () {
$("#btni").click(function () {
var dataSent = "{roll:" + 1 + ",name:\"" + "Mubashir\"}"
$.ajax({
type: "Post",
url: "EmailList.aspx/heelo",
data: dataSent,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var studentInfo = response.d;
alert(JSON.stringify(response.d));
$("#output").empty();
$.each(studentInfo, function (index, Info) {
//alert(index);
//alert(index.Info);
$("#output").append(
'<strong>Roll : </strong>' + Info.roll + ' ' +
'<strong>Name : </strong>' + Info.name + ' ' +
'<strong>Address : </strong>' + Info.address + '<br/> '
);
});
},
failure: function (msg) {
alert(msg.d);
$("#output").text(msg);
}
});
});
});
</script>
如果我将相同的方法放在myservice.asmx代码文件中,它就可以正常工作。使您的方法
静态
[WebMethod]
public static string heelo(int roll, string name)
{
return "Mubashir";
}
要了解ASP.NETAJAX页面方法为什么必须是静态的,请阅读
[WebMethod]
public static string heelo(int roll, string name)
{
return "Mubashir";
}