axios實現跨域的問題 vue實現跨域


第一次寫博客,主要是記錄自己的一些新的,不好的地方忘各位多多指點,請不要吐槽;

按踩坑順序敘述。本人對http了解太少,所以坑踩得較多。 
1.開始進行跨域時,知道vue2.0官方推薦axios進行ajax請求,大致看一下https://www.npmjs.com/package/axios axios的用法,感覺挺好理解嘛,封裝的挺好,用時發現,不對啊。跨域設置在哪?最后找到了它

proxyTable: {
   '/shopping':{//此處並非一定和url一致。
      target:'https://mainsite-restapi.ele.me/shopping',
      changeOrigin:true,//允許跨域
      pathRewrite:{
        '^/shopping': ''
      }
    }
}
此段代碼表示,如果請求地址以/shopping開頭,則自動加上target。 如:/shopping/v2/restaurant/category 等於 https://mainsite-restapi.ele.me/shopping/v2/restaurant/category 設置成功,感覺axios就是方便。走着走着發現。。。不對

  

2.get請求成功,換成post請求。坑爹啊

:8000/#/login:1 XMLHttpRequest cannot load http://cunguan.com/index.php?user&q=action/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.

  

查了半天發現直接訪問接口時,要對后端響應頭進行設置(最后發現如果用1中的方法進行跨域訪問設置則不需要在后端添加響應頭)
 
        
// 指定允許其他域名訪問
header("Access-Control-Allow-Origin:*");
// 響應類型
header("Access-Control-Allow-Methods:POST");
// 響應頭設置
header("Access-Control-Allow-Headers:x-requested-with,content-type");
添加完畢,好了錯沒了,可發現數據好像有問題啊。我訪問的是自己的接口,因為是以前的老接口,不能改所以只有硬着頭皮改前台了
 
        

3.以前的請求參數為form data怎么這次請求神奇的變為request payload,崩潰中,最后找到要添加Content-Type:application/x-www-form-urlencoded

headers: {
     'Content-Type': 'application/x-www-form-urlencoded'
}
this.$http.post('/login/index.php?user&q=action/login', {'a': 'test', 'b': '123456'}), {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
})
    .then(function (response) {
       console.log(response)
     })
     .catch(function (error) {
       console.log(error)
     })
好吧  請求默認的需要修改我認了,改過之后發現。。。我參數呢?這次好了,參數都丟了繼續查文檔吧

 

4.Content-Type:application/x-www-form-urlencoded 時參數格式的問題下面摘自 
https://github.com/mzabriskie/axios/blob/master/README.md#using-applicationx-www-form-urlencoded-format. 下面三種技能,我用了一種,輕松搞定。

 

By default, axios serializes JavaScript objects to JSON. To send data in the application/x-www-form-urlencoded format instead, you can use one of the following options.

**Browser**

In a browser, you can use the URLSearchParams API as follows:

    var params = new URLSearchParams();
    params.append('param1', 'value1');
    params.append('param2', 'value2');
    axios.post('/foo', params);
Note that URLSearchParams is not supported by all browsers, but there is a polyfill available (make sure to polyfill the global environment).
Alternatively, you can encode data using the qs library:

    var qs = require('qs');
    axios.post('/foo', qs.stringify({ 'bar': 123 }));

Node.js

In node.js, you can use the querystring module as follows:

    var querystring = require('querystring');
    axios.post('http://something.com/', querystring.stringify({ foo: 'bar' }));

You can also use the qs library.

如果到這還沒解決你的問題,不好意思,go for it 哈哈哈

此方法需要引入qs這個包,用es6語法引入也可以,即import qs from 'qs'; 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM