錯(cuò)誤信息:

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access. The response had HTTP status code 403
隨著前端框架的發(fā)展,如今前后端數(shù)據(jù)分離已經(jīng)成了趨勢(shì),也就是說(shuō),前端只需要用ajax請(qǐng)求后端的數(shù)據(jù)合成頁(yè)面,后端僅僅提供數(shù)據(jù)接口,給數(shù)據(jù)就行,好處就是前端程序員再也不用在自己的本地搭建Web服務(wù)器,前端也不需要懂后端語(yǔ)法,后端也不需要懂前端語(yǔ)法,那么簡(jiǎn)化了開(kāi)發(fā)配置,也降低了合作難度。
常規(guī)的GET,POST,PUT,DELETE請(qǐng)求是簡(jiǎn)單請(qǐng)求(相對(duì)于OPTIONS請(qǐng)求),但是OPTIONS有點(diǎn)兒特別,它要先發(fā)送請(qǐng)求問(wèn)問(wèn)服務(wù)器,你要不要我請(qǐng)求呀,要我就要發(fā)送數(shù)據(jù)過(guò)來(lái)咯(這完全是根據(jù)自己的理解寫(xiě)的,如果有誤,敬請(qǐng)諒解,請(qǐng)參考阮一峰大神原文。)
在Vue的項(xiàng)目里,Http服務(wù)采用Axios,而它正是采用OPTIONS請(qǐng)求。
如果僅僅在header里面加入: 'Access-Control-Allow-Origin':*,是并不能解決問(wèn)題的,錯(cuò)誤就是如文章開(kāi)頭所示。
這兒就需要后臺(tái)對(duì)OPTIONS請(qǐng)求額外處理。
本文以Spring MVC為例:
添加一個(gè)攔截器類(lèi):
public class ProcessInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
httpServletResponse.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
httpServletResponse.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
httpServletResponse.setHeader("X-Powered-By","Jetty");
String method= httpServletRequest.getMethod();
if (method.equals("OPTIONS")){
httpServletResponse.setStatus(200);
return false;
}
System.out.println(method);
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁(yè)題目:完美解決axios跨域請(qǐng)求出錯(cuò)的問(wèn)題-創(chuàng)新互聯(lián)
路徑分享:http://www.chinadenli.net/article4/dcdeoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷(xiāo)、網(wǎng)站導(dǎo)航、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)公司、品牌網(wǎng)站建設(shè)、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容