[英]call ajax function in javascript
我想在javascript中調用Ajax,但它給出了CallPageMethod未定義的錯誤。 如何定義? 我是阿賈克斯的新手。 你能幫助我嗎?
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.