Home> PHP Framework> ThinkPHP> body text

Solve the configuration problem of Thinkphp in the joint development of Thinkphp and vue

藏色散人
Release: 2020-08-06 13:22:55
forward
3882 people have browsed it

The following tutorial column ofthinkphp frameworkwill introduce to you the configuration issues of Thinkphp in the joint development of Thinkphp and vue. I hope it will be helpful to friends in need!

Solve the configuration problem of Thinkphp in the joint development of Thinkphp and vue

ThinkphpConfiguration issues of Thinkphp in joint development with vue:

1. Thinkphp returns data in json format by default

(1) Global settings - modify the Thinkphp configuration file config.php:

'default_return_type' => 'json',
Copy after login

default_return_type attribute’s default value is 'html', after changing it to 'json', the data returned directly in the controller method is in json format. The global configuration is valid for all "operations" in the system

(2) Separate setting - In "Operation", return data via: return json(array, 404) (the first parameter is an array or object, and the second parameter is the status code).

return json(['name' => 'thinkphp','status' => '1'], 200);
Copy after login

Output data (the results of global and individual setting output are the same):

{"name":"thinkphp","status":"1"}
Copy after login

2. Vue request cross-domain problem

When the front and back ends are separated, vue will encounter cross-domain problems when requesting directly through axios: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Solve the configuration problem of Thinkphp in the joint development of Thinkphp and vue

You need to set the request header in "Operation"

header('Access-Control-Allow-Origin: *')
Copy after login

Just copy and paste it into the first line of "Operation", like this:

Solve the configuration problem of Thinkphp in the joint development of Thinkphp and vue

When you connect this "operation" through axios, you will not be prompted for cross-domain restrictions.

Solve the configuration problem of Thinkphp in the joint development of Thinkphp and vue

The above is the detailed content of Solve the configuration problem of Thinkphp in the joint development of Thinkphp and vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!