user1950164
user1950164

Reputation:

access json with javascript

Yes, this is another one of those "accessing a json with javascript". Indulge me, I read the rest of the answers, event this one, and didn't help.

I have the following code

var display_order_message = function(res, status) {
    alert(res.status+' '+res.message+' '+res["message"]);
};


$("#ticketform").submit( function(event) {
  data = {};
  var args = {
    type:"POST",
    url:"someurldoesntmatterhere",
    data:data,
    dataType:"json",
    success: somefunctionsheredontmattereither,
    complete: display_order_message
  };
  $.ajax(args);
  event.preventDefault();
});

Now, using Firebug I can see that the json which is returned is

{"status": 200, "qa": [], "message": "order canceled", "qb": []}

The alert in the code above prints

200 undefined undefined

So, why is it that I can access .status but not .message or ["message"]? And how to I access the message?

Upvotes: 0

Views: 81

Answers (2)

Ludovic Guillaume
Ludovic Guillaume

Reputation: 3287

Put display_order_message in success arg instead of complete.

Currently, your res variable is a jqXHR which also has a status child.

See http://api.jquery.com/jquery.ajax/

Upvotes: 4

gogagubi
gogagubi

Reputation: 1015

very simple

var obj = {"status": 200, "qa": [], "message": "order canceled", "qb": []}
var message = obj.message;
var message2 = obj['message'];

both works

Upvotes: -1

Related Questions