간단한 jsonp 크로스 요청 실현

2910 단어
태그:jsonp
추후에 다시 상세히 보충하다

java 백엔드 코드

/**
 * pingan_sap jsonp  ,               
 * @param response
 * @param workgroupid
 * @param subccno
 * @param vdn
 * @param callback
 */
@RequestMapping(value = "/keywordServiceDisableGrid",method = {RequestMethod.GET })
@ResponseBody
public void keywordServiceDisableGrid(HttpServletResponse response, String workgroupid, String subccno, String vdn, String callback) {

    JSONObject retJsonObject=new JSONObject();
    try{
        boolean flag = ctiService.getServiceDisableDataGrid(workgroupid, subccno, vdn);
        if (flag) {
            retJsonObject.put("resultcode", "0");
            retJsonObject.put("resultmsg", "success");
        }else{
            retJsonObject.put("resultcode", "1");
            retJsonObject.put("resultmsg", "failure");
        }
        log.error("      ,      =  " + flag);
        }catch(Exception e){
        retJsonObject.put("resultcode", "1");
        retJsonObject.put("resultmsg", "failure");
        log.error(e.getMessage());
    }
    //     callback     js    jsonp     
    String result = callback+"("+retJsonObject.toString()+");";//      js

    //       
    PrintWriter pw= null;
    try {
        pw = response.getWriter();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(result);
    pw.print(result);
}

js 프런트엔드 코드


function onlineKeyword(){
    $.ajax({
        type : 'GET',
        dataType : 'jsonp', //        jsonp
        jsonp : "callback", //  jsonp     ,                  json js  
        url : 'http://127.0.0.1:8081/pingan_cti/interfaces/keywordServiceDisableGrid', //    
        async : false,
        data: {
            "workgroupid":'-1',
            "subccno":'1',
            "vdn":'1',
        },
        success : function (response) {
            if(response.resultcode == 0){
                Modal_Alert('       ','    ');
            }else{
                Modal_Alert('       ','    ');
            }
        },
        error: function (XMLHttpReuqest, textStautus, errothrown) {
            console.log(XMLHttpRequest.status);
            console.log(XMLHttpReuqest.readyState);
            console.log(XMLHttpRequest.responseText);
            console.log(textStautus);
            console.log(errothrown);
            Modal_Alert('       ','    ');
        }
    });
}

jsp


좋은 웹페이지 즐겨찾기