Javascript 客户端Ajax,jQuery读取JSON

Javascript 客户端Ajax,jQuery读取JSON,javascript,ajax,jquery,javascript-events,Javascript,Ajax,Jquery,Javascript Events,我正在尝试制作一个Javascript,用于获取json(IP数据)并使用AJAX从中检索数据(GEO IP),这就是我目前所拥有的 $(document).ready(function(){ var path_to_the_webservice = "http://www.pathtothescript.com/check.php"; $.ajax({ url: path_to_the_webservice, success: function(

我正在尝试制作一个Javascript,用于获取json(IP数据)并使用AJAX从中检索数据(GEO IP),这就是我目前所拥有的

$(document).ready(function(){
    var path_to_the_webservice = "http://www.pathtothescript.com/check.php";
    $.ajax({
        url: path_to_the_webservice,
        success: function(html)
        {
            if(html)
            {
                alert('3');
                $('#content').append(html);                         
            }
            else
            {
                alert('4');
            }
        }
    });
});
我得到了警告(4),为什么

基本上当您访问
http://www.pathtothescript.com/check.php
从浏览器中检索我必须解析的JSON

$.getJSON(path_to_the_json,
function(data) 
{
    $.each(data, function(i,item)
    {

    });
}
但我不知道该怎么做

json看起来像这样
http://j.maxmind.com/app/geoip.js

有什么帮助吗?谢谢

这可能是由以下原因引起的

尝试使用JSONP请求:

$.getJSON('http://example.com?callback=?', function(data) {
    console.log(data);
});

处理来自的响应


更新:

在聊天中,我们发现我前面的例子在Google Chrome中运行良好,但在Mozilla Firefox中不起作用。 虽然我玩了一点,找到了解决办法:

// Actually we can send regular AJAX request to this domain
// since it sends header Access-Control-Allow-Origin:*
// which allows cross-domain AJAX calls.
$.ajax({
    url: 'http://j.maxmind.com/app/geoip.js',
    type: 'GET',
    success: function(data) {
        // Now we have some functions to use:
        alert(geoip_country_name() + ': (' 
              + geoip_latitude() + '; ' 
              + geoip_longitude() + ')');
    },
    error: function(e) {
        console.log('Error:', e);
    },
    contentType: 'application/javascript; charset=ISO-8859-1',
    dataType: 'script'
});



我还相应地设置了一个字符集来服务。

插入
console.log(html)时的输出是什么在成功处理程序中的
if
语句之前?跨域问题?如果你想要一个解析的JSON对象,为什么要命名你的参数
html
?跨域问题,就像用你的ip访问这个,我知道跨域是什么,但我只是尝试访问和更改3-4个divs geo,ip…@row:这个URL返回JavaScript,而不是JSON。发送头
访问控制允许源:
允许跨域AJAX请求()。那么你的问题是什么呢?那么标题访问控制允许Origin:*很重要吗?UH有趣的。。。但是解析JSON呢?因为这不是带有json->array的PHP。是的,这个头很重要。但是,如果您计划在应用程序中以这种方式使用它,请小心。我已经更新了我的答案,以说明如何应对这种反应。
// Actually we can send regular AJAX request to this domain
// since it sends header Access-Control-Allow-Origin:*
// which allows cross-domain AJAX calls.
$.ajax({
    url: 'http://j.maxmind.com/app/geoip.js',
    type: 'GET',
    success: function(data) {
        // Now we have some functions to use:
        alert(geoip_country_name() + ': (' 
              + geoip_latitude() + '; ' 
              + geoip_longitude() + ')');
    },
    error: function(e) {
        console.log('Error:', e);
    },
    contentType: 'application/javascript; charset=ISO-8859-1',
    dataType: 'script'
});