说明:文章的地址url请换成自己的url;
1、ajax调用其他服务
function qryOther(){
var hasAuth = $("#hasAuth").val();
var perssion = $("#perssion").val();
var qryType = $("#qryType").val();
var nbr = $("#nbr").val();
var staffno = $("#staffno").val();
if(perssion=="0"){
if(hasAuth=="N"){
dialog("信息", "请先通过身份鉴权", 0);
return ;
}
}
var clientIp = $("#clientIp").val();
var url = "https://dpi.crm.189.cn:8056/dpi-web/accredit.htm?userName=10000_hub&operId=10000_hub&password=10000_xx&pac=27"
+"&accessTime="+date2()
+"&clientIp="+clientIp
+"&phone="+nbr;
$.ajax({
type : "get",
url : url,
async : true,
dataType : 'json',
beforeSend : function() {
loading();
},
success : function(result) {
if (result.status=="1000") {
var toUrl = " https://text?ticket=" + result.ticket + "&queryTime=" + date1() + "&queryType="+qryType;
window.open(toUrl);
} else {
dialog("信息", "查询失败", 0);
}
},
complete : function() {
closeLoad();
}
});
// var _url ='../internetTraceQuery/accredit?staffno='+staffno+"&nbr="+nbr+"&qryType="+qryType;
// location.href = encodeURI(_url);
}
2、出现问题:"{"readyState":0,"status":0,"statusText":"No Transport"}"
解决:在controller添加
response.setHeader("Access-Control-Allow-Origin", "*"); // 这里最好明确的写允许的域名
// response.setHeader("Cache-Control", "no-cache"); //可要可不要
response.setHeader("Access-Control-Allow-Methods", "*");//GET POST
//进入主页
@GetMapping(value = "/index")
public String index(String staffno, Model model,HttpServletResponse response,HttpServletRequest request) {
log.info("InternetTraceQueryController[type=sub][index][staffno = "
+ staffno + "]");
response.setHeader("Access-Control-Allow-Origin", "*"); // 这里最好明确的写允许的域名
// response.setHeader("Cache-Control", "no-cache");
response.setHeader("Access-Control-Allow-Methods", "*");//GET POST
model.addAttribute("staffno", staffno);
String clientIp = "";
if(StringUtils.isEmpty(clientIp)){
clientIp = CommUtil.getIpAddress(request);
String localIp = "0:0:0:0:0:0:0:1";
if (localIp.equals(clientIp)) {
clientIp = "127.0.0.1";
}
}
model.addAttribute("clientIp", clientIp);
return "flow/internetTraceQuery/index";
}
谷歌访问js
//查询 其他浏览器
function qryOther(){
var hasAuth = $("#hasAuth").val();
var perssion = $("#perssion").val();
var qryType = $("#qryType").val();
var nbr = $("#nbr").val();
var staffno = $("#staffno").val();
if(perssion=="0"){
if(hasAuth=="N"){
dialog("信息", "请先通过身份鉴权", 0);
return ;
}
}
var clientIp = $("#clientIp").val();
var url = "https://test?userName=10000_hub&operId=10000_hub&password=10000_xx&pac=27"
+"&accessTime="+date2()
+"&clientIp="+clientIp
+"&phone="+nbr;
$.ajax({
type : "get",
url : url,
async : true,
dataType : 'json',
beforeSend : function() {
loading();
},
success : function(result) {
if (result.status=="1000") {
var toUrl = " https://test.htm?ticket=" + result.ticket + "&queryTime=" + date1() + "&queryType="+qryType;
window.open(toUrl);
} else {
dialog("信息", "查询失败", 0);
}
},
complete : function() {
closeLoad();
}
});
}
谷歌再次访问,成功,开心(开心太早,往下看)。
由于公司服务大部分需要ie8访问,再次ie8访问,访问失败。
以下解决ie8 ajax 访问问题。后台代码不变。
jQuery.support.cors=true;
jQuery.ajaxSetup({
xhr: function() {
if(window.ActiveXObject){
return new window.ActiveXObject("Microsoft.XMLHTTP");
}else{
return new window.XMLHttpRequest();
}
}
});
crossDomain: false,//有的为true 根据需要修改值
js代码:
function qryIe(){
var hasAuth = $("#hasAuth").val();
var perssion = $("#perssion").val();
var qryType = $("#qryType").val();
var nbr = $("#nbr").val();
var staffno = $("#staffno").val();
if(perssion=="0"){
if(hasAuth=="N"){
dialog("信息", "请先通过身份鉴权", 0);
return ;
}
}
var clientIp = $("#clientIp").val();
var url = "https:/ww.htm?userName=10000_hub&operId=10000_hub&password=10000_xx&pac=27"
+"&accessTime="+date2()
+"&clientIp="+clientIp
+"&phone="+nbr;
jQuery.support.cors=true;
jQuery.ajaxSetup({
xhr: function() {
if(window.ActiveXObject){
return new window.ActiveXObject("Microsoft.XMLHTTP");
}else{
return new window.XMLHttpRequest();
}
}
});
$.ajax({
type : "get",
url : url,
async : true,
dataType : 'json',
crossDomain: false,
beforeSend : function() {
loading();
},
error: function (e) {
console.log(JSON.stringify(e));
},
success : function(result) {
if (result.status=="1000") {
var toUrl = " https://ww/query.htm?ticket=" + result.ticket + "&queryTime=" + date1() + "&queryType="+qryType;
window.open(toUrl);
} else {
dialog("信息", "查询失败", 0);
}
},
complete : function() {
closeLoad();
}
});
// var _url ='../internetTraceQuery/accredit?staffno='+staffno+"&nbr="+nbr+"&qryType="+qryType;
// location.href = encodeURI(_url);
}
然后ie再次访问 成功。 开心(再次开心太早)。
将服务发布之后,再次访问,ie再次没有权限,这个问题我花了接近3天,最终发现是浏览器设置问题,参考了网上多方文章:
打开ie浏览器设置:
工具->Internet 选项->安全->自定义级别” 将“其他”选项中的“通过域访问数据源”选中为“启用”或者“提示”.
整个过程,前前后后折磨了一个星期,痛苦万分。希望对大家有用。