No 'Access-Control-Allow-Origin' header is present on the requested resource.

報錯:Access to XMLHttpRequest at 'http://localhost:8080/user/get' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.html

最近在寫一個SpringBoot+Mybatis+Ajax的電商秒殺項目遇到bug,

  • 這是一個跨域請求的錯誤,程序啓動的時候html頁面是請求本地域名,這種請求能夠正確的達到服務端,而且也有返回,可是Ajax認定他們的域不一樣,是不安全的,會報錯,而且走不到Ajax裏面。
  • 處理方式:在SpringBoot中只要Response時刻返回,全部的域便可
  • SpringBoot提供了簡單的註解形式@CrossOrigin

再次開啓服務器後運行

這樣就不會報錯了(大笑)!
相關文章
相關標籤/搜索