When you return with Eval, you don't always get the value.

Source: Internet
Author: User

-------------------------------------------------
Public Function Ajax () {
Var_dump ($_post);d ie;
$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 ();//This is the one where 5 data records can be removed.
$this->ajaxreturn ($DATAA);
}
}

var dat=eval ("(" + msg + ")");//Use this sentence, the following will be executed, excuse me for what? I made it with thinkphp3.1.2.


Reply to discussion (solution)

Make sure MSG is JSON formatted data

Alert (msg) look at

Alert (msg) look at



Popup to display [object Object],[object object],[object object],[object Object],[object Object]

Public Function Ajax () {
Var_dump ($_post);d ie;
$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 ();//This is the one where 5 data records can be removed.
$this->ajaxreturn ($DATAA);
The type of $DATAA is already an array. Your front end can be used directly, alert (MSG. Your Data key value), no more eval.

or convert the array to a JSON string
$this->ajaxreturn (Json_encode ($DATAA));//This reurn is a JSON string, your front end to eval or other parsing JSON method parsing.
}
}

You are using the JSON conversion of thinkphp then you specify the type of JSON parameter that is returned when you post in jquery

If alert (msg)
Popup to display [object Object],[object object],[object object],[object Object],[object Object]
It means that MSG is already a parsed JS array. Obviously you do not provide the real code, because if there is no JSON parameter, $.post is not self-parsing

Since MSG has been parsed,
var dat=eval ("(" + msg + ")");
It's just wrong.

The data you returned is already JSON and does not need to be eval.

var dat=eval ("(" + msg + ")");
Switch
var dat=msg;
You can do it.

The function should be appended with a ' JSON ' to declare the JSON format returned

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.