通过jsonp实现跨域,需要请求端带上jsoncallback=jQuery110206280181081965566_1476262321380,这相当于把钥匙,返回端带着这把钥匙返回数据jQuery110206280181081965566_1476262321380(json串),所以需要请求端跟服务端都做修改。
请求示例:
返回示例:
jQuery110206280181081965566_1476262321380({"StateCode":"0","Message":"成功","Data":"0"})
请求端(js)
$.ajax({
type: "GET",
dataType : 'jsonp',
jsonp:"jsoncallback",//jsoncallback可任意取,服务端接收时一致就行,如果不一致会报jsoncallback不识别,不是jsoncallback开头的也会报不识别的错
url: 'http://'+element.vm.ip+':8034/Monitor/StartMonitorMachine',
beforeSend: function (xhr) {
xhr.setRequestHeader("Access-Control-Allow-Origin","*");
},
data: {
"configId": stdetailsvm.monitorConfigList[i].id,
"itemId": itemId,
//"startTime": new Date().toLocaleString(),
"startTime": stdetailsvm.currentTime(),
"lastTime": stdetailsvm.monitorLast,
//"ReCallUrl": window.location.host
"ReCallUrl": "http://172.16.13.10:8008",
"ip":element.vm.ip
},
success: function (data) {
if (data.Data == "0") {
alert("向服务器发送启动监控指令成功。");
} else {
alert("向服务器发送监控指令失败。"+data.Message);
}
},
error: function (XMLHttpRequest,textStatus,errorThrown) {
alert("请求数据异常,状态码:" + XMLHttpRequest.status+",Error:"+errorThrown+",textStatus:"+textStatus);
}
});
type: "GET",
dataType : 'jsonp',
jsonp:"jsoncallback",//jsoncallback可任意取,服务端接收时一致就行,如果不一致会报jsoncallback不识别,不是jsoncallback开头的也会报不识别的错
url: 'http://'+element.vm.ip+':8034/Monitor/StartMonitorMachine',
beforeSend: function (xhr) {
xhr.setRequestHeader("Access-Control-Allow-Origin","*");
},
data: {
"configId": stdetailsvm.monitorConfigList[i].id,
"itemId": itemId,
//"startTime": new Date().toLocaleString(),
"startTime": stdetailsvm.currentTime(),
"lastTime": stdetailsvm.monitorLast,
//"ReCallUrl": window.location.host
"ReCallUrl": "http://172.16.13.10:8008",
"ip":element.vm.ip
},
success: function (data) {
if (data.Data == "0") {
alert("向服务器发送启动监控指令成功。");
} else {
alert("向服务器发送监控指令失败。"+data.Message);
}
},
error: function (XMLHttpRequest,textStatus,errorThrown) {
alert("请求数据异常,状态码:" + XMLHttpRequest.status+",Error:"+errorThrown+",textStatus:"+textStatus);
}
});
服务端Net MVC
public class MachineMonitorConfigPack : PostPackBase
{
public string jsoncallback { get; set; }
public int configId { get; set; }
public int itemId { get; set; }
public string startTime { get; set; }
public int lastTime { get; set; }
public string ReCallUrl { set; get; }
}
{
public string jsoncallback { get; set; }
public int configId { get; set; }
public int itemId { get; set; }
public string startTime { get; set; }
public int lastTime { get; set; }
public string ReCallUrl { set; get; }
}
public string StartMonitorMachine(MachineMonitorConfigPack info)
{
var resp = new APIResponse<string>()
{
StateCode = StateCode.Success,
Message = "启动监控成功"
};
try
{
ImpersonateUser user = new ImpersonateUser();
ImpersonateUserExt.ImpersonateAdminUser(user);
//DateTime startTime = DateTime.Parse(info.startTime);
DateTime startTime = DateTime.Now;
DateTime endTime = startTime.AddMinutes(info.lastTime);
MachineMonitor.SetStartTime(startTime);
MachineMonitor.SetDeadTime(endTime);
MachineMonitor.SetRecalUrl(info.ReCallUrl);
MachineMonitor.SetConfigId(info.configId);
MachineMonitor.SetItemId(info.itemId);
MachineMonitor.StartMonitor();
Logger.Info("Post Start Monitor Machine success.");
Logger.Info("startTime:"+ startTime);
Logger.Info("endTime:"+ endTime);
Logger.Info("RecalUrl:"+ info.ReCallUrl);
Logger.Info("ConfigId:"+ info.configId);
Logger.Info("ItemId:" + info.itemId);
resp.Data = "0";
}
catch (Exception ex)
{
resp.StateCode = StateCode.Fail;
resp.Message += ex.ToString();
resp.Data = "1";
Logger.Info("Start Monitor Machine Exception:" + ex);
}
Logger.Info("请求StateCode:"+resp.StateCode+ ",Message:"+resp.Message);
return info.jsoncallback + "(" + new JavaScriptSerializer().Serialize(resp) + ")";
}
{
var resp = new APIResponse<string>()
{
StateCode = StateCode.Success,
Message = "启动监控成功"
};
try
{
ImpersonateUser user = new ImpersonateUser();
ImpersonateUserExt.ImpersonateAdminUser(user);
//DateTime startTime = DateTime.Parse(info.startTime);
DateTime startTime = DateTime.Now;
DateTime endTime = startTime.AddMinutes(info.lastTime);
MachineMonitor.SetStartTime(startTime);
MachineMonitor.SetDeadTime(endTime);
MachineMonitor.SetRecalUrl(info.ReCallUrl);
MachineMonitor.SetConfigId(info.configId);
MachineMonitor.SetItemId(info.itemId);
MachineMonitor.StartMonitor();
Logger.Info("Post Start Monitor Machine success.");
Logger.Info("startTime:"+ startTime);
Logger.Info("endTime:"+ endTime);
Logger.Info("RecalUrl:"+ info.ReCallUrl);
Logger.Info("ConfigId:"+ info.configId);
Logger.Info("ItemId:" + info.itemId);
resp.Data = "0";
}
catch (Exception ex)
{
resp.StateCode = StateCode.Fail;
resp.Message += ex.ToString();
resp.Data = "1";
Logger.Info("Start Monitor Machine Exception:" + ex);
}
Logger.Info("请求StateCode:"+resp.StateCode+ ",Message:"+resp.Message);
return info.jsoncallback + "(" + new JavaScriptSerializer().Serialize(resp) + ")";
}
服务端 java spring mvc
public class MachineMonitorConfigPack {
private String jsoncallback;
private String jsoncallback;
。。。
}
@RequestMapping(value = { "/StartMonitorMachine" },method = { RequestMethod.GET },produces = "application/json;charset=UTF-8")
@ResponseBody
public String StartMonitorMachine(MachineMonitorConfigPack config) {
try {
logger.info("请求StartMonitorMachine:configId="
+ config.getConfigId() + "&itemId=" + config.getItemId()
+ "&startTime=" + config.getStartTime() + "&lastTime="
+ config.getLastTime() + "&ReCallUrl="
+ config.getReCallUrl() + "&ip=" + config.getIp());
MonitorService.setConfigId(config.getConfigId());
MonitorService.setItemId(config.getItemId());
MonitorService.setRecallUrl(config.getReCallUrl());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = sdf.parse(config.getStartTime());
Date deadDate = new Date(startDate.getTime() + config.getLastTime()
* 60 * 1000);
MonitorService.setStartTime(startDate);
MonitorService.setDeadTime(deadDate);
MonitorService.setIp(config.getIp());
MonitorService.getInstance().StartMonitor();
logger.info("请求启动监控信息成功");
ret.put("StateCode","0");
ret.put("Message","成功");
ret.put("Data","0");
} catch (Exception e) {
// TODO Auto-generated catch block
logger.info("请求StartMonitorMachine Exception:"+e);
ret.put("StateCode","1");
ret.put("Message",e.toString());
ret.put("date","1");
}
return config.getJsoncallback()+"("+ret.toString()+")"; }
@ResponseBody
public String StartMonitorMachine(MachineMonitorConfigPack config) {
try {
logger.info("请求StartMonitorMachine:configId="
+ config.getConfigId() + "&itemId=" + config.getItemId()
+ "&startTime=" + config.getStartTime() + "&lastTime="
+ config.getLastTime() + "&ReCallUrl="
+ config.getReCallUrl() + "&ip=" + config.getIp());
MonitorService.setConfigId(config.getConfigId());
MonitorService.setItemId(config.getItemId());
MonitorService.setRecallUrl(config.getReCallUrl());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = sdf.parse(config.getStartTime());
Date deadDate = new Date(startDate.getTime() + config.getLastTime()
* 60 * 1000);
MonitorService.setStartTime(startDate);
MonitorService.setDeadTime(deadDate);
MonitorService.setIp(config.getIp());
MonitorService.getInstance().StartMonitor();
logger.info("请求启动监控信息成功");
ret.put("StateCode","0");
ret.put("Message","成功");
ret.put("Data","0");
} catch (Exception e) {
// TODO Auto-generated catch block
logger.info("请求StartMonitorMachine Exception:"+e);
ret.put("StateCode","1");
ret.put("Message",e.toString());
ret.put("date","1");
}
return config.getJsoncallback()+"("+ret.toString()+")"; }