I'm trying to insert into an html table data, retrieved with ajax the response arrive as:
[{"email":"marc@volvo.com","id":2,"name":"volvo"},{"email":"marc@mercedes.com","id":4,"name":"mercedes"}]
that's look fine, but when I try to access it as an object, I receive undefined. if I bring it as json type, it show the word object instead of the object content. It should be an array of 2 object, with 3 value each, no matter what i tries i can't manage to access the value, here's the javascript code (i don't know yet jquery) :
function getCompanies(){
var req = new XMLHttpRequest()
var url = "http://localhost:8080/CouponB/webapi/admin/company"
req.open("GET", url, true)
req.send()
req.onreadystatechange = function() {
if(req.readyState==4) {
if(req.status==200){
myStr = ""
var resp = req.response
alert(resp)
myJson = eval(resp)
alert(myJson)
for(var x in myJson){
alert("-----------"+x.name)
}
var respTarg = document.getElementById("companyTable")
resp.forEach(wtr)
respTarg.innerHTML = myStr
}
}
}
}
function wtd(d){
return "<td style=\"width: 70px; \">"+d+"</td>"
}
function wtr(){
myStr += wtd(this.id)+wtd(this.name)+wtd(this.email)
}