简体   繁体   English

神秘的ajax json请求问题jQuery

[英]Mysterious ajax json request problems jQuery

I want to read an rss feed converted to json format with a google api ; 我想阅读使用谷歌api转换为json格式的rss提要; i had put some alerts but i can't see them when i run my page ! 我已经放了一些警报,但是我在运行页面时看不到它们! Why ? 为什么呢

Here is my jQuery code : 这是我的jQuery代码:

function getFeed(url){
        $('#screen #content').html("");
        $.ajax({
            url: 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q='+url,
            crossDomain: true,
            dataType: 'json',
            success: function(data) {
                alert(3);
                $.each(data.entries, function(i,results){
                    alert(1);

                });
            }
        });
    }
    getFeed('http://www.nytimes.com/services/xml/rss/nyt/Science.xml');

Thanks ! 谢谢 !

Ajax requests are limited by the browser's Same Origin Policy. Ajax请求受浏览器的“相同来源策略”限制。 You can't talk directly to a server via ajax that isn't on the same domain as the page your script is running in. So, you need to use the jsonp feature from jquery ajax: 您不能通过与脚本运行页面不在同一域的ajax直接与服务器通信。因此,您需要使用jquery ajax的jsonp功能:

$(document).ready(function () {

        function getFeed(url) {
            $.ajax({
                url: 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=' + url,
                crossDomain: true,
                dataType: 'jsonp',
                success: function (data) {
                    console.log(data);

                },
                error: function (data) {
                    console.log(data);
                }
            });
        }
        getFeed('http://www.nytimes.com/services/xml/rss/nyt/Science.xml');

    });

dataType: 'jsonp' is the keyword here. dataType:“ jsonp”是此处的关键字。

You can read more about it searching 'jsonp' here: http://api.jquery.com/jQuery.ajax/ 您可以在这里阅读有关搜索“ jsonp”的更多信息: http : //api.jquery.com/jQuery.ajax/

or here: http://bob.ippoli.to/archives/2005/12/05/remote-json-jsonp/ 或此处: http : //bob.ippoli.to/archives/2005/12/05/remote-json-jsonp/

  1. It can also be possible that the host you are requesting to, doesn't allow third-party or cross-browser ajax calls. 您请求的主机也有可能不允许第三方或跨浏览器的Ajax调用。
  2. If you are expecting JSON as in result from request, use $.getJSON() . 如果期望从请求中得到JSON,请使用$.getJSON()

Thanks Rahul 谢谢拉胡尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM