[英]Call web service using json
我有兩個項目:
ASP.net網站項目:
Default.aspx
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type = "text/javascript">
function DisplayMessageCall() {
$.ajax({
type: "POST",
url: "http://localhost:24218/Service1.asmx/HelloWorld",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccessCall,
error: OnErrorCall
});
}
function OnSuccessCall(response) {
$('#<%=lblOutput.ClientID%>').html(response.d);
}
function OnErrorCall(response) {
alert(response.status + " " + response.statusText);
}
</script>
<h2>Example 1: Call Webservice using JQuery AJax (Without Input)</h2>
<asp:Button ID="btnGetMsg" runat="server" Text="Click Me" OnClientClick="DisplayMessageCall();return false;" /><br />
<asp:Label ID="lblOutput" runat="server" Text=""></asp:Label>
和ASP Web Service應用程序:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
return "Hello World";
}
}
我在ASP網站上添加了我的webService的引用,但是當我單擊按鈕時,我收到了錯誤消息。 當我輸入網址
(http://localhost:24218/Service1.asmx/HelloWorld)
在瀏覽器中,我得到以下結果:
<string xmlns="http://tempuri.org/">Hello World</string>
我使用ASP.NET 4.5。
看起來您已經配置了GET方法而不是POST方法。 如果您將$ ajax呼叫切換為:
$.ajax({
type: "GET",
. . .
那么它可能會起作用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.