tp5.1 與vue ajax請求跨域的問題


header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept");
header('Access-Control-Allow-Methods: POST,GET');

在router/router.php頭上寫上上面的header頭就可以正常調用了。

使用 axios 后導致 thinkphp 的 isAjax 一直為false;

解決辦法:

給請求頭添加 X-Requested-With 這個參數,參數值為 XMLHttpRequest

// 以 axios 為例
 
this.$axios.get('http://yourdomain.com/api/getMenus', 
{
    headers: {'X-Requested-With': 'XMLHttpRequest'}
 
}).then((res)=>{
                
    // console.log(res.data);
                
})

 


免責聲明!

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



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