Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing jQuery AJAX response

Tags:

jquery

ajax

I use the following function to post a form to via jQuery AJAX:

$('form#add_systemgoal .error').remove(); var formdata = $('form#add_systemgoal').serialize(); $.ajaxSetup({async: false});   $.ajax({          type: "POST",     url: '/admin/systemgoalssystemgoalupdate?format=html',     data: formdata,     success: function (data) {         console.log(data);        }, }); 

It posts fine but I cannot parse the respons, it logs to console as follows

{     "success": 1,     "inserted": {         "goal_id": "67",         "goalsoptions_id": "0",         "user_id": "0",         "value": "dsfdsaf",         "created": "2013-06-05 09:57:38",         "modified": null,         "due": "2013-06-17 00:00:00",         "status": "active",         "actions_total": "0",         "actions_title": "sfdgsfdgdf",         "action_type": "input",         "points_per_action": "1",         "expires": "2013-06-11 00:00:00",         "success": 1     } } 

which I believe is the response I am looking for.

However when I try to do alert(data.success); or any of the other members of the response object it is undefined.

Any advice appreciated.

like image 421
Barry Hamilton Avatar asked Jun 05 '13 09:06

Barry Hamilton


People also ask

How to use AJAX response in jQuery?

The parameters specifies one or more name/value pairs for the AJAX request. The data type expected of the server response. A function to run if the request fails. A Boolean value specifying whether a request is only successful if the response has changed since the last request.

How to get data from response object in jQuery?

var dataObject = jQuery. parseJSON(data); so you can call it like: success: function (data) { var dataObject = jQuery.


1 Answers

calling

var parsed_data = JSON.parse(data); 

should result in the ability to access the data like you want.

console.log(parsed_data.success); 

should now show '1'

like image 183
sjmarshy Avatar answered Sep 27 '22 00:09

sjmarshy