In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the example analysis of the AJAX return ajaxReturn () method in thinkphp. The article is very detailed and has a certain reference value. Interested friends must read it!
The details are as follows:
The system supports any AJAX class library, and the Action class provides an ajaxReturn method for returning data to the client after an AJAX call. And support JSON, XML and EVAL to accept data to the client, by configuring DEFAULT_AJAX_RETURN to set, the default configuration uses JSON format to return data, when selecting different AJAX class libraries, you can use different ways to return data.
If you want to use ThinkPHP's ajaxReturn method to return data, you need to follow a certain format specification of the returned data. The data formats returned by ThinkPHP include:
Status operation status
Info prompt message
Data returns data
$this- > ajaxReturn (return data, prompt information, operation status)
The returned data data can support strings, numbers, arrays, and objects. When returning to the client, it is encoded and transmitted according to different return formats. If it is in JSON format, it will be automatically encoded into a JSON string; if it is in XML mode, it will be automatically encoded into a XML string; if it is in EVAL mode, only the string data data will be output, and the status and info information will be ignored.
Here is a simple example:
$User=M ("User"); / / instantiate the User object $result = $User- > add ($data); if ($result) {/ / returns the user ID added by the client after success, and returns the prompt message and operation status $this- > ajaxReturn ($result, "New successful!" , 1);} else {/ / returns the operation status and prompt message of the error $this- > ajaxReturn (0, "add error!" , 0);} $data ['status'] = 1' info'] = 'info';$data [' size'] = 9 'url'] = $url;$this- > ajaxReturn ($data,'JSON')
Thinkphp source code:
/ * return data to client * @ access protected* @ param mixed $data * @ param String $type AJAX return data format * @ return void*/protected function ajaxReturn ($data,$type='') {if (func_num_args () > 2) {/ / compatible with 3.0.Use $args = func_get_args (); array_shift ($args); $info = array () $info ['data'] = $data; $info [' info'] = array_shift ($args); $info ['status'] = array_shift ($args); $data = $info; $type = $args?array_shift ($args):';} if (empty ($type)) $type = C ('DEFAULT_AJAX_RETURN') If (strtoupper ($type) = = 'JSON') {/ / returns the JSON data format to the client containing status information header (' Content-Type:text/html; charset=utf-8'); exit (json_encode ($data));} elseif (strtoupper ($type) = = 'XML') {/ / returns xml format data header (' Content-Type:text/xml; charset=utf-8') Exit (xml_encode ($data));} elseif (strtoupper ($type) = = 'EVAL') {/ / return executable js script header (' Content-Type:text/html; charset=utf-8'); exit ($data);} else {/ / TODO add other formats}} above are all the contents of the article "sample Analysis of the AJAX return ajaxReturn () method in thinkphp". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.