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