1
자바 스크립트에서 Ajax를 호출하려고하지만 CallPageMethod 정의되지 않은 오류가 발생합니다. 그것을 정의하는 방법? 나는 아약스의 초보자이다. 너 나 좀 도와 줄 수있어?자바 스크립트에서 ajax 함수 호출
[WebMethod]
public static bool IsLangExists()
{
return true;
}
자바 스크립트에서 Ajax를 호출하려고하지만 CallPageMethod 정의되지 않은 오류가 발생합니다. 그것을 정의하는 방법? 나는 아약스의 초보자이다. 너 나 좀 도와 줄 수있어?자바 스크립트에서 ajax 함수 호출
[WebMethod]
public static bool IsLangExists()
{
return true;
}
뒤에
이<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>
코드는 CallPageMethod 어디서나 정의되어 있습니까?
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.");
}
다음
은 어떻게해야입니다 :
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
});
}
가 서버 측 메서드의 반환 값을 얻으려면, 당신은하지 result
의 값을 확인함으로써 onSuccess
콜백을 사용해야합니다 모두 함께 보아라 :
<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>
나는 지금이 기능을 추가했으나 여전히 같은 오류를 준다. – baros
그 위치는 페이지에 추가 한 위치에 따라 다릅니다. 전체 답변을 보여주기 위해 제 대답을 업데이트하겠습니다. 귀하의 회신에 감사드립니다. –
'결과'값은 아직 정의되지 않았습니다. 코드를 디버깅했습니다. IsLangExists 메서드는 true를 반환합니다. – baros