使用eval返回后总取不到值,请问为什么
<script> <br /> $(document).ready(function(){ <br /> $('.submit').click(function(){ <br /> var name=$('.content').val(); <br /> $.post("{:U('index.php/Index/Index/ajax')}",{content:name},function(msg){ <br /> var dat=eval("(" + msg + ")");//使用这句后,下面就执行不行了,请问为什么呢 <br /> $(".show").empty(); <br /> $.each(dat,function(neirongIndex,datt){ <br /> var html = "<div class='neirong'><span>"+datt['timee']+" "; <br /> html += datt['sender']; <br /> html += ":<br/>"+datt['content']; <br /> html += ""; <br /> $('.show').append(html); <br /> }); <br /> $("textarea").val(''); <br /> }); <br /> });}); <br /></script>
-------------------------------------------------
public function ajax(){
// var_dump($_POST);die;
$data=array();
$Model = new Model();
$data['content']=$_POST['content'];
$data['time']=time();
$data['timee']=date('Y-m-d H:i:s',time());
$data['sender']="aaa";
$dd=M(msg)->data($data)->add();
if($dd){
$dataa=M('msg')->order('id desc')->limit('5')->select();//这里是可以取出5条数据记录的
$this->ajaxReturn($dataa);
}
}
var dat=eval("(" + msg + ")");//使用这句后,下面就执行不行了,请问为什么呢?我是用thinkphp3.1.2做的
回复讨论(解决方案)
请确认 msg 是 json 格式数据
alert(msg)看看
alert(msg)看看
弹出来显示[object Object],[object Object],[object Object],[object Object],[object Object]
public function ajax(){
// var_dump($_POST);die;
$data=array();
$Model = new Model();
$data['content']=$_POST['content'];
$data['time']=time();
$data['timee']=date('Y-m-d H:i:s',time());
$data['sender']="aaa";
$dd=M(msg)->data($data)->add();
if($dd){
$dataa=M('msg')->order('id desc')->limit('5')->select();//这里是可以取出5条数据记录的
$this->ajaxReturn($dataa);
/// $dataa的类型以已经是数组了。你前端可以直接使用了,alert(msg.你的数据key值),不用再EVAL。
//或者把数组转化为json字符串
$this->ajaxReturn(json_encode($dataa));//这样reurn的就是一个json字符串,你前端要EVAL或者其他解析json的方法解析。
}
}
你是使用的thinkphp的json转化 那么你在jquery的post的时候,指明下返回的类型json参数
如果 alert(msg)
弹出来显示[object Object],[object Object],[object Object],[object Object],[object Object]
就表示 msg 已是解析后的 js 数组了。显然你没有提供真实的代码,因为如果没有 json 参数,$.post 是不会自行解析的
由于 msg 已经解析,再
var dat=eval("(" + msg + ")");
就是错误的了
你返回的数据已经是json,不需要再eval。
var dat=eval("(" + msg + ")");
改为
var dat=msg;
就可以了。
function后面应该加一个'json'来声明返回的是json格式

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The arrow function was introduced in PHP7.4 and is a simplified form of short closures. 1) They are defined using the => operator, omitting function and use keywords. 2) The arrow function automatically captures the current scope variable without the use keyword. 3) They are often used in callback functions and short calculations to improve code simplicity and readability.
