gpt4 book ai didi

javascript - 有没有办法将 HTML 响应转换为 JSONP 对象或其他方式来获得成功事件?

转载 作者:行者123 更新时间:2023-11-28 00:02:54 24 4
gpt4 key购买 nike

我正在尝试通过此网站检测用户所在的城市:http://www.ipaddresslocation.org/my-ip-address.php 。到目前为止,我见过的几乎所有免费地理定位 API 都不是很准确。当执行以下代码时,我在“响应”选项卡(在 Firefox 的“网络”选项卡中)中看到了所需的所有信息,但成功事件失败,我无法访问任何数据。

$(document).ready(function(){                    
getCity();
});

var parameters = {
"Content-Type": "text/html",
'Accept-Encoding': 'gzip, deflate',
};

function getCity() {

$.ajax({
type: "GET",
url: 'http://www.ipaddresslocation.org/ip-address-locator.php',
dataType: 'jsonp',
success: function(data) {
console.log("success");
},
error: function(xhr, status, error) {
console.log("ERROR");
console.log(error.message);
}
});
}

enter image description here

enter image description here

enter image description here

最佳答案

我的代码的问题是,当我只需要在浏览器中读取 HTML 页面时,我试图让服务器做出响应。由于同源策略,我最终使用 CORS Anywhere node.js 代理将 header 添加到代理请求中,然后执行 RegEx 来获取城市。这个链接很好地解释了如何发出跨域请求:Loading cross domain endpoint with jQuery AJAX .

$(document).ready(function() {

$.ajaxPrefilter(function(options) {
if (options.crossDomain && jQuery.support.cors) {
var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
}
});

$.get(
'http://www.ipaddresslocation.org/ip-address-locator.php',
function(response) {
//regex to get the desired info
});

});

关于javascript - 有没有办法将 HTML 响应转换为 JSONP 对象或其他方式来获得成功事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31621848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com