Home  >  Article  >  Web Front-end  >  How to introduce the vue-resource module in vue.js and implement http cross-domain requests

How to introduce the vue-resource module in vue.js and implement http cross-domain requests

不言
不言Original
2018-08-23 16:14:225476browse

The content of this article is about how to introduce the vue-resource module in vue.js and implement http cross-domain requests. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

Reference the vue-resource module in the vue project

npm install vue-resource --save-dev

Reference in the main.js file

//引入 vue-resource模板
import VueResource from 'vue-resource'

Vue.config.productionTip = false;
Vue.use(VueResource);


new Vue({
  el: '#app_head',
  components: { App_head },
  template: &#39;<App_head/>&#39;,
  created(){                                 //created() 网页运行时触发
   this.$http.get("http://jsonplaceholder.typicode.com/users")   //使用http请求
     .then((data) =>{                                  //请求成功后返回结果给 data
       console.log(data);
     })
  }
});

There may be cross-domain request issues
You can add this code to the php file

<?php
      header("Access-Control-Allow-Origin: *");
?>

Related recommendations:

Detailed explanation of how Vue-cli implements cross-domain requests

Introduction to the configuration method of router in vue.js

The above is the detailed content of How to introduce the vue-resource module in vue.js and implement http cross-domain requests. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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