【发布时间】:2017-06-20 00:55:15
【问题描述】:
如何仅使用 javascript 获取国家代码,我不允许使用 jQuery。
我看过一些类似http://ipinfo.io的网站,但所有例子都使用JQuery getJson,我可以实现相同的方法吗:
var country_code = null;
$.getJSON('http://ipinfo.io/' + userip, function(data){
country_code = data.country;
alert(country_code);
});
我尝试了以下方法,但它返回了一个页面:
var request = new XMLHttpRequest();
request.onload = function(elements) { console.log(elements); };
request.open("get", "http://ipinfo.io", true);
request.send();
【问题讨论】:
-
你可以使用
fetch还是必须使用XHR? -
你用谷歌搜索过这个吗?
-
IE 不支持抓取
-
@cruiser,谷歌搜索已经一个小时了
-
见youmightnotneedjquery.com/#json。他们可以替代用纯 javascript 编写的 JQuery 方法。
标签: javascript