thinkphp echo return problem with JSON data

Source: Internet
Author: User
I've been tinkering with a jquery autocomplete plugin today
I'm using Ajax to get the pull-down data back in the background, and then the egg hurts.
First of all, I'm using the thinkphp framework.
First of all, we must return the JSON data.
And then I just return json_encode($end,JSON_UNESCAPED_UNICODE);
Results The front-end JS plugin has been an error, presumably to say that the returned JSON data is not legal
And then I tried it for a long time. echo json_encode($end,JSON_UNESCAPED_UNICODE);
The result can be, I also baffled its solution Ah, please master explain, things clearly exactly the same as a echo a return how there is a difference

Reply content:

I've been tinkering with a jquery autocomplete plugin today
I'm using Ajax to get the pull-down data back in the background, and then the egg hurts.
First of all, I'm using the thinkphp framework.
First of all, we must return the JSON data.
And then I justreturn json_encode($end,JSON_UNESCAPED_UNICODE);
Results The front-end JS plugin has been an error, presumably to say that the returned JSON data is not legal
And then I tried it for a long time.echo json_encode($end,JSON_UNESCAPED_UNICODE);
The result can be, I also baffled its solution Ah, please master explain, things clearly exactly the same as a echo a return how there is a difference

The JS callback requires a value, and PHP must only output a JSON string to receive it.
This is the concept of HTTP protocol, request sending and response acceptance. There must be a value in the content returned by response. Ajax to get to.
and PHP is output JSON string statement has the Echo function has exit () Die ()
As for the return of the controller layer inside the thinkphp. Just returned to the thinkphp core controller class. Finally, if the controller does not have any output, the response content is empty.

SF has a similar problem before: the difference between Echo and return
Say it again, OK:

returnis to return the result to the PHP program.
echois the AJAX response.

Really drunk, the landlord saw the front-end JS error, you can not by the way to see the return value of Ajax? What is the problem is not a glance at things.

The right approach should be

exit(json_encode($end,JSON_UNESCAPED_UNICODE));

returnis returned in the function.
echois the 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.