Idle all right, holiday knock Vue request data, summarize under the Vue spanning problem
The first kind. Server server does not support cross-domain requests
1. when cross-domain cannot be requested, we can modify the dev:{} section in Index.js under the Config folder of the project.
Add the following code:
proxytable: { '/api ': { ' http://api.douban.com/v2 ', true , pathrewrite: { ' ^/api ': '}} ,
Set Target to the domain name we need to access.
2. Then set the global properties in main.js :
Vue.prototype.HOST = '/api '
3. We can use this domain name in the global, as follows:
var this. HOST + '/movie/in_theaters ' this . $http. Get (URL). Then (res = { this. Movielist = res.data.subjects; = = { console.info (' call failed '); });
The second type: server-side support for cross-domain
String data = jsonutil.tojsonnonnull (pubxtzdbos); = Response.getoutputstream (); Out.write (Data.getbytes ("UTF-8")); // encode with UTF-8 Response.setheader ("Access-control-allow-origin", "*"); // tell the browser how to encode
is to be able to support cross-domain then you can use JSONP to request it. Jsonp is actually added by adding a script tag,
The requested data is also JS format. Axios currently does not support, you can only manually create the script tag, the address of the request assigned to the SRC attribute of the script tag, and finally added to the head tag, wait until the request to return and then delete the label:
Jsonprequest:function(A, E) { This. _ajaxtimer && Cleartimeout ( This. _ajaxtimer); This. _ajaxtimer = SetTimeout (function () { varRequest_script = document.createelement (' script '); Request_script.setattribute ("id", "request_script"); REQUEST_SCRIPT.SRC= ' http://xxxx ' + ' &t= ' +Date.now (); Window.callback=function(response) {//Remove ScriptDocument.body.removeChild (document.getElementById (' Request_script '))); Console.log (response.content); } document.body.appendChild (Request_script); }, 500); },
True, local development is suitable for the first kind. Then you can use Axios for AJAX requests,
However, this can only be used in development mode. It is recommended to use Nginx as an agent when packaging the deployment.
, I have not tried the second kind, but also to review the summary of the data, haha
Vue solves Axios cross-domain issues