我有以下问题,有一个ASP。NET Web服务项目(*.asmx),具有多个函数,是第二个项目,也是Ajax,它应该使用jQuery Ajax调用服务(无服务引用)。Web服务项目:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class MathService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod]
public int Add(int x, int y)
{
return x + y;
}
}和
<script type="text/javascript" src="Scripts/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
url: "http://localhost:47204/Services/MathService.asmx/Add",
data: "{'x': '5', 'y': '10'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (res) {
$("#divMathService").text(res.d);
},
error: function () {
alert("ALARM!");
}
});
});
</script>这个- localhost:47204 -在我调试项目时托管web服务项目。我特别指出了完整的URL,以便在客户端项目中使用它。项目成功了,一切都很好。项目客户端如下所示:
<script type="text/javascript" src="Scripts/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
url: "http://localhost:47204/Services/MathService.asmx/Add",
data: "{'x': '5', 'y': '10'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (res) {
$("#divMathService").text(res.d);
},
error: function () {
alert("ALARM!");
}
});
});
</script>但是这个项目没有显示任何结果,只有警示,我不明白为什么。也许远程调用的权限有问题?
我不能附加项目,但可以发送到电子邮件,如果有人想看项目。
发布于 2011-09-28 06:07:29
先尝试将数据格式化为有效的json:
'{"x": 5, "y": 10}'此外,您的方法需要知道如何将响应作为json发送:
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public int Add(int x, int y)https://stackoverflow.com/questions/7575239
复制相似问题