我正在尝试从 javascript 调用 c# 函数,我尝试过的代码如下。
C# 代码
[System.Web.Services.WebMethod]
public void myFun()
{
Response.Redirect("http://google.com");
}
在javascript中,我尝试了以下两个代码
function CallMe(src, dest) {
//First code i have tried
//var ctrl = document.getElementById(src);
// call server side method
//PageMethods.myFun(CallSuccess, CallFailed, dest);
//Second code i have tried
$.ajax({ type: "POST",
url: myFun, contentType: "application/json; charset=utf-8",
//data: "{passedVal:" + JSON.stringify(clientRequest) + "}", dataType: "json",
success: function (result, status) {
alert("success");
},
error: function (xhr, status, error) {
alert("ERROR");
}
});
}
function CallSuccess(res, destCtrl) {
alert("success");
}
function CallFailed(res, destCtrl) {
alert("fail");
}
但两者都没有醒来。谁能告诉我我做错了什么?