jQuery 在javascript中调用ajax函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15920734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
call ajax function in javascript
提问by baros
I want to call Ajax in javascript but it gives CallPageMethod undefined error. How to define it? and I'm newbie in Ajax. Can you help me?
我想在 javascript 中调用 Ajax,但它给出了 CallPageMethod 未定义的错误。如何定义?我是 Ajax 的新手。你能帮助我吗?
<script type="text/javascript">
function ValidateDelete() {
var result = CallPageMethod("IsLangExists", success, fail);
if (result == true) {
return confirm('Do you want to continue ?')
}
else alert('You can not delete this record');
}
function success(response) {
//alert(response.d);
}
function fail(response) {
//alert("An error occurred.");
}
</script>
<asp:GridView ID="grdList" OnRowCommand="grdList_RowCommand">
<Columns>
<asp:BoundField DataField="LangId" HeaderText="LangId" Visible="false" />
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:ImageButton ID="imgBtnDelete" runat="server" CommandName="_Delete" CommandArgument='<%#Eval("LangId")%>' ImageUrl="~/Image/delete_icon.gif" OnClientClick="return ValidateDelete();"
ToolTip="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code behind
背后的代码
[WebMethod]
public static bool IsLangExists()
{
return true;
}
回答by Andreas Grech
Is your CallPageMethoddefined anywhere?
您的CallPageMethod是否在任何地方定义?
function CallPageMethod(methodName, onSuccess, onFail) {
var args = '';
var l = arguments.length;
if (l > 3) {
for (var i = 3; i < l - 1; i += 2) {
if (args.length != 0) args += ',';
args += '"' + arguments[i] + '":"' + arguments[i + 1] + '"';
}
}
var loc = window.location.href;
loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "default.aspx" : loc;
$.ajax({
type: "POST",
url: loc + "/" + methodName,
data: "{" + args + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: onSuccess,
fail: onFail
});
}
To get the return value of your server-side method, you will need to use the onSuccess
callback, not by checking the value of result
:
要获取服务器端方法的返回值,您需要使用onSuccess
回调,而不是通过检查 的值result
:
function ValidateDelete() {
CallPageMethod("IsLangExists", success, fail);
}
function success(response) {
if (response.d) {
return confirm('Do you want to continue ?');
}
alert('You can not delete this record');
}
function fail(response) {
//alert("An error occurred.");
}
Here's how it should all look together:
以下是它们应该如何组合在一起:
<script type="text/javascript">
function CallPageMethod(methodName, onSuccess, onFail) {
var args = '';
var l = arguments.length;
if (l > 3) {
for (var i = 3; i < l - 1; i += 2) {
if (args.length != 0) args += ',';
args += '"' + arguments[i] + '":"' + arguments[i + 1] + '"';
}
}
var loc = window.location.href;
loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "default.aspx" : loc;
$.ajax({
type: "POST",
url: loc + "/" + methodName,
data: "{" + args + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: onSuccess,
fail: onFail
});
}
function ValidateDelete() {
CallPageMethod("IsLangExists", success, fail);
}
function success(response) {
if (response.d) {
return confirm('Do you want to continue ?');
}
alert('You can not delete this record');
}
function fail(response) {
//alert("An error occurred.");
}
</script>
<asp:GridView ID="grdList" OnRowCommand="grdList_RowCommand">
<Columns>
<asp:BoundField DataField="LangId" HeaderText="LangId" Visible="false" />
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:ImageButton ID="imgBtnDelete" runat="server" CommandName="_Delete" CommandArgument='<%#Eval("LangId")%>'
ImageUrl="~/Image/delete_icon.gif" OnClientClick="return ValidateDelete();"
ToolTip="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>