Home  >  Article  >  Backend Development  >  javascript - Why does the data returned by ajax prompt object?

javascript - Why does the data returned by ajax prompt object?

WBOY
WBOYOriginal
2016-09-29 09:33:021012browse

After running, why does the prompt result show [object Object], [object Object] instead of specific data?
This result should be like this [{title: "3333333333333"}, {title: "3333333333333"}]
Then I tried it againalert(obj.title) It prompts undefined. Why is this?

<code>$res=$xiao->field("title")->select();
$this->ajaxReturn($res);


ajax("{:U('zhuye/zhuye')}",oV1,function(str){
   var obj = eval("("+str+")");
   alert(obj);
});</code>

Reply content:

After running, why does the prompt result show [object Object], [object Object] instead of specific data?
This result should be like this [{title: "3333333333333"}, {title: "3333333333333"}]
Then I tried it againalert(obj.title) It prompts undefined. Why is this?

<code>$res=$xiao->field("title")->select();
$this->ajaxReturn($res);


ajax("{:U('zhuye/zhuye')}",oV1,function(str){
   var obj = eval("("+str+")");
   alert(obj);
});</code>

Use jquery to solve your problem, please read the documentation for details

obj is an Array of two Objects, not an Object

Tryalert(obj[0].title); alert(obj[1].title);

Try it. What comes out should be the array form you mentioned.

<code>var obj = JSON.parse(str)

alert(obj);</code>

Just add a return data type, dataType:json

Print it out and read it. . . .

should be

<code>   alert(obj[0].title);
</code>

Your data is already an Object. When you are eval or JSON.prase, you are calling obj.toString();

You will know what the problem is if you look at the difference between JavaScript literal objects and json.

The return type is not in json format, right?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn