标签: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前端代码
<!-- 上线导入的服务禁用语,跨域请求cti -->
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
<button type="button" class="btn btn-info btn-sm" onclick="onlineKeyword()"><span class="ace-icon fa glyphicon-plus icon-on-right bigger-110"></span>上线</button>