java解決ajax異步跨域問題

前端寫法:

function qrCodeNameEdit(ajaxUrl,newName,storeId){
    $.ajax({
        type:'get',
        url : '/text',
        dataType : 'jsonp',
        jsonp:'callbackparam',
        success:function(json) {
            console.log(json);
        },
        error:function(e) {
            console.log(e);
        }
    });
}

重點在於:dataType使用jsonp   jsonp下定義callbackparam函數

後端寫法:

@ResponseBody
@RequestMapping(value = "/text",method = RequestMethod.GET)
public JSONPObject text(HttpServletRequest request){
    String callbackparam = request.getParameter("callbackparam");
    boolean flag = channelCodeService.edit(newName);
    return new JSONPObject(callbackparam,flag);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章