Lai32290
Lai32290

Reputation: 8578

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

Upvotes: 0

Views: 40

Answers (1)

Niels
Niels

Reputation: 49929

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
    }
});

Upvotes: 1

Related Questions