-2

PHP returns the following to javascript

[{"uniq":"358","ulot":"S803.45","lname":"Algeri","fname":"Paul","minit":"","nickname":"","vid":"479480","hphone":"352-555-3242","cphone":"","email":"fubar@comcast.net","password":"popskids","fulltime":"1","expiration":"2018-12-31","posted":"2014-01-01 00:00:00"},

{"uniq":"357","ulot":"S803.45","lname":"Algeri","fname":"Patricia","minit":"","nickname":"Pat","vid":"788636","hphone":"352-555-3242","cphone":"","email":"snafu@comcast.net","password":"popskids","fulltime":"1","expiration":"2018-12-31","posted":"2014-01-01 00:00:00"}]

Which looks to me like it should be an array of JSON objects.

Yet when I try to iterate through it with array.forEach I get the response array.forEach is not a function.

So, either this is not an array or I'd doing something wrong.

Help is appreciated.

Jacob
  • 77,566
  • 24
  • 149
  • 228
Dave Davis
  • 574
  • 1
  • 4
  • 14
  • 4
    Could you share the code where you iterate the array? – Rinsad Ahmed Sep 20 '18 at 00:24
  • 5
    JSON text needs to be parsed, ie `JSON.parse(incomingData)` otherwise you are just dealing with a text string – Patrick Evans Sep 20 '18 at 00:24
  • @PatrickEvans it also is automatically parsed in some cases – zerkms Sep 20 '18 at 00:27
  • 2
    This question needs to show how the response is received and how you are attempting to iterate the array. Without that, nobody can tell you where you're going wrong. Please update it with your JavaScript code – Phil Sep 20 '18 at 00:36

1 Answers1

2

[Edited]

var res = JSON.parse(data);

NoobX
  • 125
  • 6
  • 3
    he didn't tag this as jquery. you shouldn't make assumptions. moreover, he mentioned `arr.forEach` meaning he's already using ES6, so need to introduce jQ here. – mpen Sep 20 '18 at 00:31
  • 1
    @mpen `Array.prototype.forEach` has been around since ES 5.1 ~ https://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.18 – Phil Sep 20 '18 at 00:38
  • 2
    @Phil Even more reason not to use jQuery :P I thought it was newer than that... we only dropped IE8 support recently. Looks like IE9 has it! – mpen Sep 20 '18 at 00:43
  • While this answer is _probably_ correct, it could do with some explanation of the potential problem and how your code solves it. – Phil Sep 20 '18 at 00:56
  • Thats my bad, question was unclear to me thats why i wrote what i got initially. I was thinking that the guy maybe trying to iterate on the string received as a response thats why i suggested Json Parse. – NoobX Sep 20 '18 at 09:58
  • For some reason I wasn't thinking straight. I was trying to enumerate the objects before parsing the PHP result. It was a simple case or parsing first and then enumerating. Thanks Usman! – Dave Davis Sep 21 '18 at 13:57