1. 程式人生 > >jFinal解決跨域問題 No 'Access-Control-Allow-Origin' header is present on the requested resource

jFinal解決跨域問題 No 'Access-Control-Allow-Origin' header is present on the requested resource

前端ajax請求後端java實現跨域問題,看了很多方法說前端這是jsonp什麼的並沒有解決,

		$.ajax({
			type:"post",
			url:"http://sdfgadsgfasdg/myPrize",
			async:true,
			dataType: 'json',
        	       //crossDomain: true,
			success:function(data){
				console.log(data)

		});
後端程式碼
    public void myPrize() {
        getResponse().addHeader("Access-Control-Allow-Origin", "*");
        List<ActGamePrize> actGamePrizes = richService.myPrize(openid);
        List<Redpack> redpackList = richService.myRedPrize(openid);

        if(actGamePrizes.size()==0&&redpackList.size()==0){
            setAttr("test",9);
        }else {
            setAttr("li",actGamePrizes);
            setAttr("lis",redpackList);
            System.out.println(redpackList+"=====================");
        }
        //String jsonp = callback+"("+ JsonKit.toJson(json)+")";//返回的json 格式要加callback()
        renderJson();

    }
最主要的就是
getResponse().addHeader("Access-Control-Allow-Origin", "*");
這句就可以解決