【发布时间】:2015-01-08 10:01:33
【问题描述】:
我在 c# asp.net 中有一个 webmethod 和一个 jquery 代码来调用该方法。如果我将代码放在 myservice .asmx 之类的 webService 页面中,它可以正常工作。我从同一个母版页调用它。但是当我从 .aspx 页面调用它时,它根本不起作用。我在按钮单击事件上调用此 Web 服务。在两种情况下都会触发按钮单击,我通过 javascript 中的警报进行了检查。
调用webmethod的我的主页代码如下:
<%-- 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>
EmailList.aspx.cs页面上的webmethod是这样的:
[WebMethod]
public string heelo(int roll, string name)
{
return "Mubashir";
}
//WebService
如果我在 myservice.asmx 代码文件上放置相同的方法,它就可以正常工作。
【问题讨论】:
标签: javascript c# jquery asp.net web-services