Javascript $.getJSON,回调不';跑不动 问题是:对我的$.getJSON请求的回调没有运行。

Javascript $.getJSON,回调不';跑不动 问题是:对我的$.getJSON请求的回调没有运行。,javascript,jquery,coffeescript,Javascript,Jquery,Coffeescript,在页面加载时,没有任何内容记录到控制台或在页面上更新,但当函数粘贴到控制台时,它会正确执行 jQuery: $(function() { $.getJSON('http://freegeoip.net/json/', function(location, textStatus, jqXHR) { console.log("callback running"); console.log(textStatus); console.lo

在页面加载时,没有任何内容记录到控制台或在页面上更新,但当函数粘贴到控制台时,它会正确执行

jQuery:

 $(function() {
    $.getJSON('http://freegeoip.net/json/', function(location, textStatus, jqXHR) {
      console.log("callback running");
      console.log(textStatus);
      console.log(jqXHR);
      $('#region-name').html(location.region_name);
    });
 });
  console.log(typeof $ !== "undefined" && $ !== null);

  console.log($.getJSON != null);
函数日志true之后的两个控制台日志

因此,上述版本被缩减。这是完整的脚本

#Geo.Coffee
$ ->
    $.getJSON(
        'http://freegeoip.net/json/?callback=?',
        (location, textStatus, jqXHR) ->  # example where I update content on the page.
            console.log "callback running"
            console.log textStatus
            console.log jqXHR  
            $('#region-name').html location.region_name  
            $('#areacode').html location.areacode
            $('#ip').html location.ip  
            $('#zipcode').html location.zipcode  
            $('#longitude').html location.longitude  
            $('#latitude').html location.latitude 
            $('#country-name').html location.country_name  
            $('#country-code').html location.country_code
            $('#city').html location.city 
            $('#region-code').html location.region_code
            $('container main content').append "<p>#{location.country_code}</p>"
            localStorage['loc'] = location.country_code
            if localStorage.loc is "US" then alert "Your From The US."  
    )#.fail(-> alert "fail").done( (loc) -> alert "done")
    console.log localStorage.loc
console.log $?
console.log $.getJSON? 
html:


正确的小提琴:

您的元素id是问题所在,请删除

演示:


看起来你的咖啡脚本有缩进问题

$ ->
$.getJSON(
          'http://freegeoip.net/json/?callback=?',
          (location, textStatus, jqXHR) ->  # example where I update content on the page.
              console.log "callback running"
              console.log textStatus
              console.log jqXHR  
              $('#region-name').html location.region_name  
              $('#areacode').html location.areacode
              $('#ip').html location.ip  
              $('#zipcode').html location.zipcode  
              $('#longitude').html location.longitude  
              $('#latitude').html location.latitude 
              $('#country-name').html location.country_name  
              $('#country-code').html location.country_code
              $('#city').html location.city 
              $('#region-code').html location.region_code
              $('container main content').append "<p>#{location.country_code}</p>"
              localStorage['loc'] = location.country_code
              if localStorage.loc is "US" then alert "Your From The US."  
          )#.fail(-> alert "fail").done( (loc) -> alert "done")
$->
$.getJSON(
'http://freegeoip.net/json/?callback=?',
(位置、文本状态、jqXHR)->#我更新页面内容的示例。
console.log“正在运行回调”
console.log textStatus
console.log jqXHR
$(“#地区名称”).html location.region_name
$('#areacode').html location.areacode
$('#ip').html location.ip
$('#zipcode').html location.zipcode
$('#longitude').html location.longitude
$('#latitude').html location.latitude
$(“#国家名称”).html location.country_name
$(“#国家代码”).html location.country_代码
$('#city').html location.city
$(“#地区代码”).html location.region_代码
$('container main content')。追加“{location.country\u code}

” localStorage['loc']=location.country\u代码 如果localStorage.loc为“US”,则向“您的邮件来自美国”发出警报 )#.失败(->警报“失败”).完成((loc)->警报“完成”)

演示:

您需要发出jsonp请求


$.getJSON('http://freegeoip.net/json/?callback=?',

我也遇到了同样的问题。原来是我的addblock。uBlock Origin阻止了几个ip服务。禁用它后,一切正常。

你能给我们看一下fiddle吗?你的jsidle适合我吗?我不熟悉coffeescript…我收到一个错误,说
未捕获引用错误:textStatus未定义
@ArunPJohny ad编辑编译的js。不确定那是编译的js,因为回调中的
位置
引用了窗口。位置请参阅添加
?回调=?
并不能解决我的问题。此外,如果在小提琴中删除它,它仍然可以工作。为什么需要添加jsonp?这是小提琴的问题,但不是实际问题。请参阅我的问题估计update@agconti考虑到这就是我们所要做的,而且这个答案是有效的,我认为这是真实的problem@agconti这就是你给我们的问题……我们只能按你给我们的去做。我在达洛佐的要求下加了一把小提琴,但弄错了。对不起,小提琴不能完全代表我的问题。
<p id="region-name"></p>
<p id="areacode"></p>
<p id="ip"></p>
<p id="zipcode"></p>
<p id="longitude"></p>
<p id="latitude"></p>
<p id="country-name"></p>
<p id="country-code"></p>
<p id="city"></p>
<p id="region-code"></p>
<p id="region-name"></p>
$(function () {
    $.getJSON('http://freegeoip.net/json/?callback=?', function (location, textStatus, jqXHR) {
        console.log("callback running");
        console.log(textStatus);
        console.log(jqXHR);
        $('#region-name').html(location.region_name);
    });
});
$ ->
$.getJSON(
          'http://freegeoip.net/json/?callback=?',
          (location, textStatus, jqXHR) ->  # example where I update content on the page.
              console.log "callback running"
              console.log textStatus
              console.log jqXHR  
              $('#region-name').html location.region_name  
              $('#areacode').html location.areacode
              $('#ip').html location.ip  
              $('#zipcode').html location.zipcode  
              $('#longitude').html location.longitude  
              $('#latitude').html location.latitude 
              $('#country-name').html location.country_name  
              $('#country-code').html location.country_code
              $('#city').html location.city 
              $('#region-code').html location.region_code
              $('container main content').append "<p>#{location.country_code}</p>"
              localStorage['loc'] = location.country_code
              if localStorage.loc is "US" then alert "Your From The US."  
          )#.fail(-> alert "fail").done( (loc) -> alert "done")