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.
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:
When you connect this "operation" through axios, you will not be prompted for cross-domain restrictions.