ajax - Codeigniter: how to foreach in javascript with a returned array -


I need a loop through the array returned in Javascript but it does not work, it Loop through

This is Javascript code

  $ Ajax ({url: "& lt ?? php echo site_url ('home / getsubcats'));; & gt;", type: 'POST', data: form_data, success: function (msg) {for (var i in Msg) {warning (message [i]);} // $ ('# main_content') .html (msg);}});  

And this is the controller (this is the right data, so the query is not the problem);

  function getsubcats () {$ this-> gt; Load-> Model ('site_model'); If ($ this-> Input-> Post ('Ajax')): $ catid = $ this- & gt; Input-> Post ('id'); $ $ - this- & gt; Site_model- & gt; Returns the received cats ($ cated); end if; }  

You may have to add returnType: 'json' If your code returns JSON, then your $ On the AJAX object object.

If your code is given an end to the single characters, then it means msg is a string and there is no array.

Additionally, for (var i = 0; i & lt; msg.length; i ++) as for loops In addition, the inherited attributes will be included - so when using JavaScript, frameworks that extend Object.prototype or Array.prototype, you may have trouble.


Comments