Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 有没有一种方法可以将HTML响应转换为JSONP对象,或者有另一种方法可以获得成功事件?_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 有没有一种方法可以将HTML响应转换为JSONP对象,或者有另一种方法可以获得成功事件?

Javascript 有没有一种方法可以将HTML响应转换为JSONP对象,或者有另一种方法可以获得成功事件?,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在尝试通过以下网站检测用户的城市:。到目前为止,我看到的几乎所有免费地理定位API都不是很准确。当执行以下代码时,我会在Response选项卡(Firefox中的Network选项卡)中看到我需要的所有信息,但成功事件失败,我无法访问这些数据 $(document).ready(function(){ getCity(); }); var parameters = { "Content-Type": "text/html", 'Acc

我正在尝试通过以下网站检测用户的城市:。到目前为止,我看到的几乎所有免费地理定位API都不是很准确。当执行以下代码时,我会在Response选项卡(Firefox中的Network选项卡)中看到我需要的所有信息,但成功事件失败,我无法访问这些数据

$(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);
      }
    });
}


当第三方API没有设置对开发人员友好的东西时,就会发生这种情况。您可以使用
complete
回调代替
success
回调,然后访问
XHR
响应的
responseText
属性:

$(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',
      complete: function(jqXHR, txtStatus) {
        var myData = jqXHR.responseText;
      },
      error: function(xhr, status, error) {
        console.log("ERROR");
        console.log(error.message);
      }
    });
}

我的代码的问题是,当我只需要在浏览器中读取HTML页面时,我试图让服务器响应。由于同源策略,我最终使用CORS Anywhere node.js代理向代理请求添加头,然后使用RegEx获取城市。此链接非常适合解释如何进行跨域请求:


尝试实现一个有目的的错误(比如在url上),看看是否进入了错误回调。如果你不知道,你就会知道问题出在那里,你可以回到这里回答我。你是说做一个测试来检查url是否被破坏了?当我在codepen中运行这个程序时,错误消息会被打印出来,但是我在Firefox请求窗口中得到了正确的位置。是的,这就是我的意思。你能上传Firefox中返回的屏幕截图吗?看起来myData没有定义,错误信息仍然会打印到控制台上。我不知道为什么。这只是API返回的方式。你能做一个
console.log(jqXHR)吗在完整的回调中?这将告诉你那里有哪些属性以及这些数据在哪里。我可以看到它的所有属性。看起来我无法从响应中显示的HTML文件中检索任何内容,因为该对象没有这样做的属性。是这样还是我忽略了什么?(对象{readyState:4,getResponseHeader:.ajax/v.getResponseHeader(),getAllResponseHeaders:.ajax/v.getAllResponseHeaders(),setRequestHeader:.ajax/v.setRequestHeader(),OverrideMetype:.ajax/v.OverrideMetype(),statusCode:.ajax/v.statusCode(),abort:,然后:。延迟/d.then(),还有10个…})。您在响应选项卡中看到的内容应该可以在
jqXHR.responseText
中找到。你不认为这是一种选择?也许应该是同步的?看看这个答案:
$(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
    });

});