gpt4 book ai didi

javascript - 在javascript中调用ajax函数

转载 作者:行者123 更新时间:2023-11-30 10:32:40 27 4
gpt4 key购买 nike

我想在 javascript 中调用 Ajax,但它给出了 CallPageMethod undefined 错误。怎么定义呢?我是 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>

代码隐藏

[WebMethod]
public static bool IsLangExists()
{
return true;
}

最佳答案

是你的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
});
}

要获取服务器端方法的返回值,您需要使用 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.");
}

下面是它应该如何组合在一起:

<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>

关于javascript - 在javascript中调用ajax函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15920734/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com