简体   繁体   中英

How to get data with jQuery ajax?

I'm try using jQuery Ajax for popularize a jqxGrid , but is not success

my jQuery code:

$.ajax(
    {        
        url: "Cliente.aspx/GetClient",
        type: "POST",
        data: {},
        dataType: "json",
        success: function (msg) {
            alert(msg);
        },
        error: function(msg) {
            alert("error");  //msg is returning error
        }
    });

I'm try get data with Entity Framework

    [WebMethod]
    public static string GetClient()
    {
        string dados;
        using (SysContext db = new SysContext())
        {
            dados = new JavaScriptSerializer().Serialize(db.Clients.ToList());
        }

        return dados;
    }

Where is my error? why

Look over here: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

You are missing:

contentType: "application/json; charset=utf-8",

Which is needed to go to the Webmethod. So it will be:

$.ajax(
{        
    url: "Cliente.aspx/GetClient",
    type: "POST",
    data: {},
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (msg) {
        alert(msg);
    },
    error: function(msg) {
        alert("error");  //msg is returning error
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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