问题:

我有一些Javascript JQuery代码每隔5分钟对服务器进行一次Ajax调用,这是为了保持服务器会话的存活并保持用户登录。我在JQuery中使用$.ajax()方法。 这个函数似乎有一个’错误’属性,我试图在用户的互联网连接断开时使用,以便KeepAlive脚本继续运行。 我正在使用以下代码:

var keepAliveTimeout = 1000 * 10;

function keepSessionAlive()
{
    $.ajax(
    {
        type: 'GET',
        url: 'http://www.mywebapp.com/keepAlive',
        success: function(data)
        {
            alert('Success');

            setTimeout(function()
            {
                keepSessionAlive();
            }, keepAliveTimeout);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            alert('Failure');

            setTimeout(function()
            {
                keepSessionAlive();
            }, keepAliveTimeout);
        }
    });
}

当我运行它时,我会在屏幕上每10秒钟在一个警告框中弹出“成功”,这很好。 但是,一旦我拔下网线,我就什么也得不到,我期待错误功能被调用并看到“故障”警告框,但没有任何反应。

假设’错误’功能仅适用于从服务器返回的非’200’状态代码,我是否正确? 有没有办法在进行Ajax调用时检测网络连接问题?

I have some Javascript JQuery code that does an Ajax call to the server every 5 mins, it’s to keep the server session alive and keep the user logged in. I’m using $.ajax() method in JQuery.This function seems to have an ‘error’ property that I’m trying to use in the event that the user’s internet connection goes down so that the KeepAlive script continues to run.I’m using the following code:When I run it, I’ll get ‘Success’ popup on the screen in an alert box every 10 seconds which is fine.However, as soon as I unplug the network cable, I get nothing, I was expecting the error function to get called and see a ‘Failure’ alert box, but nothing happens.Am I correct in assuming that the ‘error’ function is only for non ‘200’ status codes returned from the server?Is there a way to detect network connection problems when making an Ajax call?

问题:

当我的网站是100%jQuery时,我曾经这样做过:

$.ajaxSetup({
    global: true,
    error: function(xhr, status, err) {
        if (xhr.status == 401) {
           window.location = "./index.html";
        }
    }
});

为401错误设置全局处理程序。 现在,我使用带有$resource$http angularjs来向服务器发出我的(REST)请求。 有没有办法类似地设置角度的全局错误处理程序?

When my website was 100% jQuery, I used to do this:to set a global handler for 401 errors.Now, I use angularjs with $resource and $http to do my (REST) requests to the server.Is there any way to similarly set a global error handler with angular?

问题:

为了添加一些基本的错误处理,我想重写一段代码,该代码使用jQuery的$ .getJSON从Flickr中提取一些照片。 这样做的原因是$ .getJSON不提供错误处理或使用超时。

由于$ .getJSON只是$ .ajax的包装,因此我决定重写该内容并感到惊讶,它可以完美工作。

现在,乐趣开始了。 当我故意导致404(通过更改URL)或导致网络超时(由于未连接到互连网)时,错误事件根本不会触发。 我对我做错了事感到茫然。 非常感谢您的帮助。

这是代码:

$(document).ready(function(){

    // var jsonFeed = "http://api.flickr.com/services/feeds/photos_public.gne"; // correct URL
    var jsonFeed = "http://api.flickr.com/services/feeds/photos_public.gne_______"; // this should throw a 404

    $.ajax({
        url: jsonFeed,
        data: { "lang" : "en-us",
                "format" : "json",
                "tags" : "sunset"
        },
        dataType: "jsonp",
        jsonp: "jsoncallback",
        timeout: 5000,
        success: function(data, status){
            $.each(data.items, function(i,item){
                $("<img>").attr("src", (item.media.m).replace("_m.","_s."))
                          .attr("alt", item.title)
                          .appendTo("ul#flickr")
                          .wrap("<li><a href=\"" + item.link + "\"></a></li>");
                if (i == 9) return false;
            });
        },
        error: function(XHR, textStatus, errorThrown){
            alert("ERREUR: " + textStatus);
            alert("ERREUR: " + errorThrown);
        }
    });

});

我想补充一点,当jQuery在1.4.2版本时,这个问题被问到了

To add some basic error handling, I wanted to rewrite a piece of code that used jQuery’s $.getJSON to pull in some photo’s from Flickr.The reason for doing this is that $.getJSON doesn’t provide error handling or work with timeouts.Since $.getJSON is just a wrapper around $.ajax I decided to rewrite the thing and surprise surprise, it works flawlessly.Now the fun starts though.When I deliberately cause a 404 (by changing the URL) or cause the network to timeout (by not being hooked up to the interwebs), the error event doesn’t fire, at all.I’m at a loss as to what I’m doing wrong.Help is much appreciated.Here’s the code:I’d like to add that this question was asked when jQuery was at version 1.4.2

问题:

我正在从事我的这个个人项目,只是出于娱乐目的,我想阅读位于http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml的xml文件并解析xml和用它来转换货币之间的价值。

到目前为止,我已经拿出下面的代码来阅读xml,这是非常基本的代码,但出现以下错误。

XMLHttpRequest无法加载****。 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问原始站点“ http://run.jsbin.com ”。

$(document).ready( 
    function() {     
        $.ajax({          
            type:  'GET',
            url:   'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml',
            dataType: 'xml',              
            success: function(xml){
                alert('aaa');
            }
         });
    }
);

我没有发现我的代码有什么问题,所以我希望有人可以指出我的代码有什么问题以及如何解决。

I am working on this personal project of mine just for fun where I want to read an xml file which is located at http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml and parse the xml and use it to convert values between the currencies.So far I have come up with the code below which is pretty basic in order to read the xml but I get the following error.XMLHttpRequest cannot load ****.No ‘Access-Control-Allow-Origin’ header is present on the requested resource.Origin ‘ http://run.jsbin.com ‘ is therefore not allowed access.I don’t see anything wrong with my code so I am hoping someone could point out what I am doing wrong with my code and how I could fix it.

问题:

我正在尝试获取新闻网站的供稿。 以为我会使用Google的feed API将feedburner供稿转换为json。 以下网址将以json格式从Feed中返回10个帖子。 http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http://feeds.feedburner.com/mathrubhumi

我用下面的代码来获取上述URL的内容

$.ajax({
  type: "GET",
  dataType: "jsonp",
  url: "http://ajax.googleapis.com/ajax/services/feed/load",
  data: {
    "v": "1.0",
    "num": "10",
    "q": "http://feeds.feedburner.com/mathrubhumi"
  },
  success: function(result) {
    //.....
  }
});

但它不起作用,出现以下错误

XMLHttpRequest无法加载http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http%3A%2F%2Ffeeds.feedburner.com%2Fmathrubhumi 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问源“ http:// localhost ”。

我该如何解决?

I’m trying to fetch the feed of a news website.Thought I’d use google’s feed API to convert the feedburner feed into json.The following url will return 10 posts from the feed, in json format.http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http://feeds.feedburner.com/mathrubhumiI used the following code to get the contents of above urlbut it’s not working and I’m getting the following errorXMLHttpRequest cannot load http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http%3A%2F%2Ffeeds.feedburner.com%2Fmathrubhumi .No ‘Access-Control-Allow-Origin’ header is present on the requested resource.Origin ‘ http://localhost ‘ is therefore not allowed access.How do I fix this?