1. 程式人生 > >記一次Jfinal開發錯誤解決

記一次Jfinal開發錯誤解決

記一次Jfinal開發錯誤解決

錯誤如下:Failed to load http://localhost/login/1-test1-123456: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘null’ is therefore not allowed access.
在這裡插入圖片描述

我是將原來部署在伺服器上的程式碼移植到本地電腦上開啟,然後出現這樣的錯誤了,需要寫一個全域性攔截器加上"Access-Control-Allow-Origin", "*"這樣一句程式碼

全域性攔截器程式碼如下

package com.tool.Interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;

public class HeaderInterceptor implements Interceptor {

	public void intercept(Invocation inv) {
		      inv.getController().getResponse().setHeader("Access-Control-Allow-Origin", "*");;
	       
	      
	          inv.invoke();    //在攔截器裡面必須有invoke方法,否則會導致後臺無法通過renderJson傳遞資料至前端
	   
		
	}

} 

這樣問題就得到了解決。