[英]Call a C# webmethod from Ajax
我需要調用此類中定義的Web方法
<%@ WebService Language="C#" Class="emt7anReyady.myService" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Linq;
using System.Web.Security;
namespace emt7anReyady
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class myService : System.Web.Services.WebService
{
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public void test()
{
Context.Response.Redirect("http://www.google.com");
}
}
}
我已經像這樣打了一個Ajax電話
function getData() {
$.ajax({
type: "POST",
url: "myService.asmx/test",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
alert("succesed")
},
failure: function () {
alert("faild")
}
});
}
呼叫失敗的問題,在chrome控制台中,我得到了這個!
您由於使用Response.Redirect
而獲得ThreadAbort Response.Redirect
。
目前尚不清楚您要使用此代碼實現的目標-如果要代理其他站點,則需要讀取和轉發響應...
這是因為您在WebMethod中使用了Response.Redirect。 如果您確實要重定向到其他頁面,請從成功塊開始。
.
.
success: function () {
window.open('http://www.google.com');
},
.
.
您還可以使用document.location而不是window.open
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.