简体   繁体   English

为什么JQuery AJAX调用不起作用?

[英]Why JQuery AJAX call is not working?

The following is my JQuery AJAX method: 以下是我的JQuery AJAX方法:

function meFun() {
    alert('enter');

    $.ajax({
        type: "POST",
        url: "About.aspx/GetRes",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            // Do something interesting here.
            alert(msg);
        }
    });
    alert('end');

}

The following is my button code: 以下是我的按钮代码:

<input type="button" onclick="meFun();" value="Click me" id="btn" />

Folling is my function in About.aspx 滚动是About.aspx中的功能

Public Function GetRes() As Boolean
    Return True
End Function

The meFun() method is called successfully but doesn't call the GetRes() and the AJAX call doesn't return any response. meFun()方法已成功调用, GetRes()调用GetRes() ,并且AJAX调用未返回任何响应。

Any idea why? 知道为什么吗? Also, please suggest a good way to debug these sorts of AJAX problems. 另外,请提出一种调试此类AJAX问题的好方法。

First, make sure you have the WebMethod attribute on your server side GetRes function (this is in C#, I don't know how you do that in VB) and make GetRes static (or whatever the equivalent in VB is). 首先,确保服务器端具有GetRes函数的WebMethod属性(这在C#中,我不知道您如何在VB中做到这一点)并使GetRes静态(或VB中的任何等效功能)。

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

Then, add the error handlers to your jQuery ajax call: 然后,将error处理程序添加到您的jQuery ajax调用中:

$.ajax({
        type: "POST",
        url: "About.aspx/GetRes",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            // Do something interesting here.
            alert(msg);
        },
        error: function (data) {
        }
    });
    alert('end');

EDIT: to debug, set a breakpoint in the error handler and inspect the data parameter. 编辑:调试,在错误处理程序中设置一个断点,并检查data参数。 All browsers (well, I can vouch for IE, Firefox (with Firebug) and Chrome) have good script debuggers. 所有浏览器(我可以担保IE,Firefox(带有Firebug)和Chrome)都具有良好的脚本调试器。 If an error occurs, there will be a property in data , the name of which eludes me at the moment, that explains the error in detail. 如果发生错误,则data中将存在一个属性,此属性目前尚不明确,该属性详细说明了该错误。

you are returning a data in a STRING FORMAT WHILE YOUR AJAX CALL EXPECTING A JSON FORMAT SO AT THE SERVER SIDE CONVERT TRUE TO JSON FORMAT BY using newtonsoft library or inbuilt jsonconversion mechanism of asp.net 当您的AJAX调用预期在服务器端使用JSON格式的AJAX调用时,您将在STRING格式中返回数据通过使用newtonsoft库或asp.net的内置jsonconversion机制将TRUE转换为JSON格式

as mentioned by svajger you have to declare it as webmethod and it must be static the return type is string prefered 如svajger所提到的,您必须将其声明为webmethod,并且必须为静态,返回类型为字符串型

and at the success handler you have to parse json data by JSON.parse method 在成功处理程序中,您必须通过JSON.parse方法解析json数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM