Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split json array response for each input field

Sorry for the repost I just need it badly and it's important! I have other posts with the same but I flagged the question answered and I have still not figure out how to fix!

I have this ajax response :

[{"error":"uname","message":" \u05e9\u05dd \u05de\u05e9\u05ea\u05de\u05e9 \u05d0\u05d9\u05e0\u05d5 \u05d9\u05db."},{"error":"email","message":" \u05d0\u05e0\u05d0 \u05d4\u05db\u05e0\u05e1 \u05d0\u05d9\u05de\u05d9\u05dc"},{"error":"password","message":" \u05e9\u05d3\u05d4 \u05d6\u05d47"},{"error":"oldpassinp","message":" \u05d4\u05e7\u05e9 \u05e1\u05d9\u05e1\u05de\u05d0 \u05e9\u05dc."}]

and my input fields like this :

<input type="text" id="fname" name="fname" value="" class="inplaceError"/><span id="fname_error"></span>

the span holds the message errors.

I use this in client side :

 var data_array = JSON.parse("["+data+"]");
 // tryed either var data_array = JSON.parse(data); not working too

  for (var i in data_array )
  {
     $("#"+data_array[i].error+"_error").html(data_array[i].message);
     $("#"+data_array[i].error).css({"border":"1px solid red"});
  }    

what wrong with that code ? It's not adding the errors for each field please any help.

I am javascript newbie.

like image 236
user2635001 Avatar asked Jul 14 '26 13:07

user2635001


1 Answers

If the ajax request is of datatype: 'json' then the response will a json array

$.each(data, function(idx, error){
    $("#"+error.error+"_error").html(error.message);
})
like image 51
Arun P Johny Avatar answered Jul 17 '26 20:07

Arun P Johny