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

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

WBOY
Release: 2016-09-29 09:33:02
Original
1077 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login

Just add a return data type, dataType:json

Print it out and read it. . . .

should be

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

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?

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template