我有以下問題,有一個ASP。 NET Web服務項目(* .asmx),有幾個函數,並且是第二個項目,也是ASP .NET,它應該使用jQuery Ajax(無服務引用)調用服務。 Web服務項目:從jQuery Ajax遠程調用ASP.Net 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>
這 - 本地主機:47204 - 主機網絡服務項目,當調試項目。 我特別指出了完整的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>
但是,這個項目沒有顯示任何結果,只有警惕,我不明白爲什麼。也許遠程通話的權限問題?
我不能附加項目,但如果有人想看項目,可以發送電子郵件。
你是在談論一個「遠程調用」和如果這意味着服務器將託管在與客戶不同的域上,那麼您的代碼將無法工作;實現這一目標的唯一方法是使用JSON-P,我認爲使用ASMX WebService構建它很困難(或幾乎不可能) –
我建議使用[fiddler](http://www.fiddler2 .com/fiddler2 /)來調試你的ajax請求。你將能夠看到你的錯誤的原因 –