0

After submitting my form (with jQuery Ajax request), I get an array back in the following structure (copied from the console), when i expand the first object:

  • [Object, Object, Object]
  • 0: Object
  • msg0: "ok"

I need to be able to refer to msg0 (and msg1, msg2 etc.) in the array - with JS/jQuery - and the corresponding message (e.g. "ok") for a conditional statement.

for example:

if (msg1 === "ok") { 
    // do stuff 
}

How can I get to those values? Any help much appreciated! This is what code I have so far:

var someData = $('#someForm').serialize(); // form with data
var btnAjaxSubmit = $('.btnAjaxSubmit'); // button (type: button) to submit data

$('.btnAjaxSubmit').click(function() {
  var request = $.ajax({
    url: "https://somesite.com/data.asp",
    method: "POST",
    data: someData
  });

  // getting response 
  request.done(function(status) {
    console.log(status['response']);

    // response ok
    if (status['response'] === "ok") {
      alert('status is "updated"')
        // session timeout
    } else if (status['response'] === "timeout") {
      alert('log in again.');
      // all other cases
    } else {
      alert('this is the other case');
    }
  });

  // no response at all
  request.fail(function(status) {
    alert('request failed!');
  });
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Melvin
  • 329
  • 4
  • 20
  • See duplicate question: [how to get json key and value in javascript?](http://stackoverflow.com/questions/18910939/how-to-get-json-key-and-value-in-javascript) – Yogi Mar 14 '16 at 16:04
  • so it's the same thing with ajax? – Melvin Mar 14 '16 at 16:17
  • I really don't think it's the same thing, I think i have to access one level deeper... I get returned an array with several objects, object[0], object[1] etc. and they have their respective key and value - and I need to get those... – Melvin Mar 14 '16 at 16:22
  • These should point you in the right direction: [How to get JSON Key and Value?](http://stackoverflow.com/questions/7073837/how-to-get-json-key-and-value) and MDN Reference [Property accessors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors) – Yogi Mar 14 '16 at 16:33
  • Thanks but that's not really working for me, I get error messages like "unexpected token u" and "unexpected token o" and when I click on the link of the error message it says [Object object] – Melvin Mar 14 '16 at 17:22
  • The question doesn't provide enough information, which is why nobody has answered. The code is just a very generic ajax call sending "someform" to a handler at "somesite" and returning some unknown object. Inside your .done() function try adding this line: console.log(JSON.stringify(status,false," ")); What do you see in the console? – Yogi Mar 14 '16 at 19:48
  • thanks for bearing with me! yeah i know, the original code is already very complex, thus i tried to keep it as generic as possible. i have some other issues in the code which might influence this one... i'll come back here when i worked the other stuff out. thanks again. – Melvin Mar 15 '16 at 10:34

0 Answers0