Home  >  Article  >  PHP Framework  >  Solve the configuration problem of Thinkphp in the joint development of Thinkphp and vue

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

藏色散人
藏色散人forward
2020-08-06 13:22:363962browse

The following tutorial column of thinkphp framework will 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',

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);

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

{"name":"thinkphp","status":"1"}

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: *')

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!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete