Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery POST Request - returning JSON

I want to use the jQuery $.ajax to make a POST call sending some information (via POST like: page.aspx?var1=value....).

But I also want jQuery to handle that the service is returning JSON so that I get back a JSON object.

var data = {name: _name, ...};

var request = $.ajax({
    url: url,
    type: "post",
    data: data,
    //dataType: "json"
});

As soon as I use dataType: "json", which allows me to receive the JSON object I get an parseerror on the request!

Hope you can help me out with that!

THANKS IN ADVACE!

like image 296
Nik Avatar asked Feb 28 '13 08:02

Nik


1 Answers

From the requested url you have to make data in JSON format like

echo json_encode($response);

and then you will get that response JSON in success function like this:

       $.ajax({
            type:"POST",
            url: "your_url",
            data:data,
            success: function (response){
                var arr = $.parseJSON(response);

            }
        });
like image 158
Vimal Patel Avatar answered Sep 18 '22 12:09

Vimal Patel