Vue solves Axios cross-domain issues

Source: Internet
Author: User

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

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.