ElementUI項目請求SpringBoot后台項目時提示:Access to XMLHttpRequest at **from origin ** has been blocked by CORS policy


場景

搭建ElementUI前端項目后提示:

Access to XMLHttpRequest at **from origin ** has been blocked by CORS policy

 

 

這是因為在請求后台SpringBoot接口時出現了跨域請求問題。

本來打算是搭建好前端項目后再js中進行ajaxq請求數據,但是會因為跨域被拒絕。

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。

實現

所以使用axios進行后台數據的請求

安裝axios

npm install axios

 

 

然后打開入口程序main.js添加axios

import axios from 'axios'

 

 

 

然后打開webpack.config.js進行url的代理配置

  

devServer: {
    host: '127.0.0.1',
    port: 8010,
    proxy: {
      '/api/': {
        target: 'http://127.0.0.1:8088',
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    },

 

 

 

以上配置代表項目的啟動端口為8010,ElementUI在向后台請求Url時,就會將/api/的請求想target中執行的地址去請求

所以我們可以在頁面App.vue中這樣去調用后台數據接口

//頁面初始化的時候,去調用
        created: function(){
            debugger
            this.getData()
        },
        methods: {
            //通過ajax去請求服務端,獲取數據
            getData() {
                debugger
                let url = "/api/user/selectAllLimit?offset=2&limit=1" ;
                this.$axios.get(url).then((res) => {

                    this.tableData = res.data;//把傳回來數據賦給packData

                }).catch(function(error){

                    console.log(error);

                })
            }

 

請求效果

 

 


免責聲明!

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



猜您在找 Access to XMLHttpRequest at '…' from origin '…' has been blocked by CORS policy 解決本地瀏覽器運行項目時的跨域問題-Access to XMLHttpRequest at"xxx/xxx" from origin 'null' has been blocked by CORS policy Access to XMLHttpRequest at *** from origin‘***' has been blocked by CORS policy: Response to preflight request ... Access to XMLHttpRequest at xxxx from origin ‘null‘ has been blocked by CORS policy: Access to XMLHttpRequest at 'XXX' from origin 'XX' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present o AJAX跨域請求解決方法 Access to XMLHttpRequest at '6%E6E6%B%9A%9' from origin from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol scheme Access to XMLHttpRequest at '*url*' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. GET *url* net::ERR_FAILED Access to XMLHttpRequest at 'http://localhost:8090/user/getotp' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. 【跨域問題】Access to XMLHttpRequest at ‘A’from ‘B’has been blocked by CORS policy :... No ‘Access-Control-Allow-Origin’ header...(Chrome瀏覽器) VUE 出現Access to XMLHttpRequest at 'http://192.168.88.228/login/Login?phone=19939306484&password=111' from origin 'http://localhost:8080' has been blocked by CORS policy: The value of the 'Access-Contr
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM