Home > Web Front-end > JS Tutorial > body text

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

不言
Release: 2018-08-23 16:14:22
Original
5447 people have browsed it

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
Copy after login

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);
     })
  }
});
Copy after login

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

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!