Thinkphpechoreturn returns json data

Source: Internet
Author: User
I have been playing a jqueryautocomplete plug-in today. I used ajax to get the drop-down data from the background, then it hurts. first, I use the thinkphp framework. first, I must return json data. then I will returnjson_encode ($ end, JSON_UNESCAPED_UNICODE );... A jquery autocomplete plug-in has been playing around today
I used ajax to get the drop-down data from the background.
First, I use the thinkphp framework.
First, it must return json data.
Then I will return json_encode($end,JSON_UNESCAPED_UNICODE);
The front-end js plug-in always reports an error, which means that the returned json data is invalid.
Then I tried to use it after a long time. echo json_encode($end,JSON_UNESCAPED_UNICODE);
The result is okay, and I can't explain it. please explain how the same echo and return are different.

Reply content:

A jquery autocomplete plug-in has been playing around today
I used ajax to get the drop-down data from the background.
First, I use the thinkphp framework.
First, it must return json data.
Then I willreturn json_encode($end,JSON_UNESCAPED_UNICODE);
The front-end js plug-in always reports an error, which means that the returned json data is invalid.
Then I tried to use it after a long time.echo json_encode($end,JSON_UNESCAPED_UNICODE);
The result is okay, and I can't explain it. please explain how the same echo and return are different.

JavaScript callback requires a value. php must output a json string to receive the value.
This is the concept of http protocol, request sending and response acceptance. The returned content of response must have a value. Ajax can be obtained.
Php outputs json strings with the echo function and exit () die ()
For the return of the controller layer in thinkphp. Only the thinkphp core controller class is returned. Finally, if the controller does not have any output, the response content is empty.

There was a similar problem before SF: The difference between echo and return
Again:

returnReturns results to the PHP program.
echoIs the Ajax response.

I was so drunk that I couldn't take a look at the ajax return value if I saw a front-end js error? What is the problem.

The correct method should be

exit(json_encode($end,JSON_UNESCAPED_UNICODE));

returnIs returned in the function.
echoIs output to the page.

TP has a $ this-> ajaxReturn (); method, which can be used directly.

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.