Porównaj ceny domen i usług IT, sprzedawców z całego świata

Wywołanie funkcji Ajax w javascript


Chcę wywołać Ajax w javascript, ale to daje CallPageMethod niezdefiniowany błąd. Jak to ustalić? i jestem nowy w Ajax. Czy mógłbyś mi pomóc?
<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>

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

Zaproszony:
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Twój

CallPageMethod
http://weblogs.asp.net/craigsh ... .aspx
zdefiniowane gdzieś?
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
});
}

Aby uzyskać wartość zwracaną przez metodę po stronie serwera, musisz użyć wywołania zwrotnego
onSuccess
zamiast sprawdzania wartości
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.");
}

Tak to wszystko powinno wyglądać razem:
<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>

Aby odpowiedzieć na pytania, Zaloguj się lub Zarejestruj się