Home > Web Front-end > JS Tutorial > Detailed explanation of Vue using axios cross-domain request data instance

Detailed explanation of Vue using axios cross-domain request data instance

小云云
Release: 2018-05-15 11:11:48
Original
3582 people have browsed it

axios does not have a jsonp cross-domain request method by default. Generally speaking, the popular approach is to solve cross-domain problems in the background, that is, backend developers add cross-domain header information. This article mainly introduces in detail the problem of Vue using axios to request data across domains. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.

For example,

header,response.setHeader("Access-Control-Allow-Origin", www.allow-domain.com)
Copy after login

in java, but many times, the backend does not want to modify it for some reasons or has already written the jsonp interface and needs to adapt to different platforms. At this time, the front end can introduce dependencies separately to resolve That’s the question.

Introduce dependencies

cnpm install jsonp
Copy after login

Import into vue file

import jsonp from 'jsonp'
Copy after login

Usage method (you can refer to the documentation of jsonp on npm)

jsonp("http://cross-domain.com",//替换网址
  {
    //jsonp的回调函数名
    name: 'success_jsonpCallback'
  },

  //注意第一个参数是 err,第二个参数是 data
  function (err, data) { 
    console.log(data);
  });
Copy after login

Related recommendations:

About vue2.0 setting proxyTable to use axios for cross-domain requests

Vue2.0 axios front-end and back-end login interceptor

The most complete axios guide

The above is the detailed content of Detailed explanation of Vue using axios cross-domain request data instance. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template