Thinkphp of the Ajaxreturn method before and after the version

Source: Internet
Author: User

Before the version of THINKPHP2 is now changed to thinkphp3.2 already on the version, found Ajaxreturn This method returned data is not the same, now do the next record

THINKPHP2 Ajaxreturn Implementation of the original code

protected functionAjaxreturn ($data,$info= ",$status=1,$type= ' ')    {        //ensures that the log can be saved after Ajax is returned        if(C (' Log_record '))Log::Save (); $result=Array(); $result[' status '] =$status; $result[' info '] =$info; $result[' data '] =$data; if(Empty($type))$type= C (' Default_ajax_return '); if(Strtoupper($type) = = ' JSON ') {            //returns the JSON data format to the client that contains state information            Header("content-type:text/html; Charset=utf-8 "); Exit(Json_encode ($result)); }ElseIf(Strtoupper($type) = = ' XML '){            //return XML Format Data            Header("Content-type:text/xml; Charset=utf-8 "); Exit(Xml_encode ($result)); }ElseIf(Strtoupper($type) = = ' EVAL '){            //returns the executable JS script            Header("content-type:text/html; Charset=utf-8 "); Exit($data); }Else{            //TODO Add other formats        }    }

The following is thinkphp3.2 's Ajaxreturn code.

protected functionAjaxreturn ($data,$type= ' ') {        if(Empty($type))$type= C (' Default_ajax_return '); Switch(Strtoupper($type)){             Case' JSON '://returns the JSON data format to the client that contains state information                Header(' Content-type:application/json; Charset=utf-8 '); Exit(Json_encode ($data));  Case' XML '://return XML Format Data                Header(' Content-type:text/xml; Charset=utf-8 '); Exit(Xml_encode ($data));  Case' JSONP '://returns the JSON data format to the client that contains state information                Header(' Content-type:application/json; Charset=utf-8 '); $handler=isset($_get[C (' Var_jsonp_handler ')]) ?$_get[C (' Var_jsonp_handler ')]: C (' Default_jsonp_handler ')); Exit($handler.‘ ('. Json_encode ($data).‘);‘);  Case' EVAL '://returns the executable JS script                Header(' content-type:text/html; Charset=utf-8 '); Exit($data); default://used to extend other returned format dataHook::listen (' Ajax_return ',$data); }    }

The final 2.0 version of Ajaxreturn returned the character class data, and I used Jquery.parsejson () This method to convert the character data to JSON format at the front end, and this data also has two values

$info = ', $status = 1, both of which are returned by default
The 3.2 version of Ajaxreturn returns directly to the JSON data and does not have to do any conversion, directly can be used

What's the problem, thank you for pointing out

Thinkphp of the Ajaxreturn method before and after the version

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.