$.getScript("http://ip.ws.126.net/ipquery", function () { console.log(localAddress);//返回对象Object{city:"南昌市",province:"江西省"} province = localAddress["province"]; province = province.replace(/省/, ''); city = localAddress["city"]; city = city.replace(/市/, ''); console.log(province);//江西 console.log(city);//南昌 });
只需更改你的url变量中 http// 为
//
,当页面加载http时,ajax请求将使用http协议,如果加载https,浏览器将ajax请求协议设置为https。即,您的代码应如下所示:
$.getScript("//ip.ws.126.net/ipquery", function () { console.log(localAddress);//返回对象Object{city:"南昌市",province:"江西省"} province = localAddress["province"]; province = province.replace(/省/, ''); city = localAddress["city"]; city = city.replace(/市/, ''); console.log(province);//江西 console.log(city);//南昌 });