This is my JSON String:
{"success":true,"docSearch":
[
{"fName":"Sam",
"lName":"Mehra",
"email":"[email protected]",
"mobile":"7859857230",
"userImage":"IMG/patient.png",
"address":"412/A Ganesh Nagar 2 gali no.1 Laxmi Nagar Near Ajay cycles, Mona Pg",
"special":"Allergist, Dentist, Anesthesiologist, Cardiologist",
"deg":"MBBS, BDS, BAMS, BUMS","experience":"3","fees":"3"},
{"fName":"Samanyu",
"lName":"Mehra",
"email":"[email protected]",
"mobile":"7859857230",
"userImage":"IMG/regis.jpg",
"address":"412/A Ganesh Nagar 2 gali no.1 Laxmi Nagar Near Ajay cycles, Mona Pg",
"special":"Allergist, Dentist, Anesthesiologist, Cardiologist, Dermatologist",
"deg":"MBBS, BDS, BAMS, BUMS, BHMS, BYNS",
"experience":"3",
"fees":"3"
}
]
}
I want to access the values in jQuery.So far I did this:
success: function(data,textStatus,jqXHR){
if(data.success){
var img=document.createElement('img');
$(data).each(function(index,item){
$("#ajaxResponse").html("");
$("#ajaxResponse").append("<b>First Name:</b> " + data.docSearch[index].fName+"<br>");
$("#ajaxResponse").append("<b>Last Name:</b> " + data.docSearch[index].lName+"<br>");
$("#ajaxResponse").append("<b>Email:</b> " + data.docSearch[index].email+"<br>");
$("#ajaxResponse").append("<b>Experience:</b> " + data.docSearch[index].experience+"<br>");
$("#ajaxResponse").append("<b>Fees:</b> " + data.docSearch[index].fees+"<br>");
img.src=data.docSearch[index].userImage;
$("#ajaxResponse").append(img);
});
}
If anyone reading this knows how to access JSON strings like this, please help me out. Note: This JSON string can also contain 1 value like:
{"success":true,"docSearch":
[
{"fName":"Sam",
"lName":"Mehra",
"email":"[email protected]",
"mobile":"7859857230",
"userImage":"IMG/patient.png",
"address":"412/A Ganesh Nagar 2 gali no.1 Laxmi Nagar Near Ajay cycles, Mona Pg",
"special":"Allergist, Dentist, Anesthesiologist, Cardiologist",
"deg":"MBBS, BDS, BAMS, BUMS","experience":"3","fees":"3"}
]
}
EDIT 1:
success: function(data,textStatus,jqXHR){
//doc Name was correct so we have some information to display
if(data.success){
var img=document.createElement('img');
var br=document.createElement('br');
$("#ajaxResponse").html("");
$(data.docSearch).each(function(index,item){
$("#ajaxResponse").append("<b>First Name:</b> " + item.fName+"<br>");
$("#ajaxResponse").append("<b>Last Name:</b> " + item.lName+"<br>");
$("#ajaxResponse").append("<b>Email:</b> " + item.email+"<br>");
$("#ajaxResponse").append("<b>Usernames:</b> " + item.username+"<br>");
$("#ajaxResponse").append("<b>Experience:</b> " + item.experience+"<br>");
$("#ajaxResponse").append("<b>Fees:</b> " + item.fees+"<br>");
img.src=item.userImage;
$("#ajaxResponse").append(img);
$("#ajaxResponse").append(br);
});
}
//display error message
else {
$("#ajaxResponse").html("<div><b>No Record Found!!</b></div>");
}
},
I added a <'br'> tag so that there is a line break between both the content that's getting displayed but, there is no line break and it is also not displaying 2 images. For the first iteration, it just reads it and left it like that only and for the 2nd iteration, it is displaying the image. I want both the data 2 display image. Please HELP !!
EDIT 2:
My problem got solved by keeping the var img=document.createElement('img'); in the loop so that whenever it sets a path for the image it it will display the image.
$(d)here ? Isnt't that should be$(data)..?dataType: "json"to your ajax ?var img=document.createElement('img'); var br=document.createElement('br');linside each loop that should work.