浏览器有时会显示所需的页面的JSON文本,而不是 [英] Browser sometimes displays JSON text instead of desired page

查看:257
本文介绍了浏览器有时会显示所需的页面的JSON文本,而不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在经历了间歇性的问题,在我的Rails应用程序,而我无法搞清楚是怎么回事。当用户登录时,他们看到一个包含一些JavaScript code的执行AJAX调用某一操作的仪表盘。偶尔,而不是看到仪表盘上,当用户登录时,他们看到的JSON响应从文本,而不是仪表盘(在Chrome)的作用或者下载一个.json文件(火狐)。这是间歇性的,通常不会发生,但它一旦发生真的很烦人。

I have been experiencing an intermittent issue in my Rails app, and I'm having trouble figuring out what is going on. When the user logs in, they see a dashboard that contains some JavaScript code which performs an AJAX call to an action. Occasionally, instead of seeing the dashboard, when the user logs in, they see the JSON response text from the action instead of the dashboard (in Chrome) or they download a .json file (Firefox). It's intermittent and doesn't usually happen, but it's really annoying when it does occur.

下面是一些的code一个简单化的版本:

Here's a dumbed down version of some of the code:

JS在模板头运行:

$(function () {
    var remoteLink = $('#remoteLink');
    remoteLink.live("ajax:complete", function () {
        setTimeout(function () {
           loadCount();
        }, 30000);
    });    

    loadCount();

    function loadCount() {
      remoteLink.click();
    }
});

和模板中的链接:

<%= link_to 'get count (hidden)', {:controller => 'something', :action => 'count'}, 
            :id => 'remoteLink', :class => 'hidden', :remote => true, 'data-type' => 'json' %>

和控制器动作:

def count
    render :json => get_counts_function_returning_a_hash
end

我的直觉是,这是一个竞争条件 - 也许与使用的setTimeout的? - 但我一直无法验证预感。谁能告诉我什么可能会在这里吗?我在其他网站上野看到了这一点,也是 - 也间歇性的,一般不存在,但恼人的,当它发生

My hunch is that it's a race condition -- perhaps related to the use of setTimeout? -- but I haven't been able to verify that hunch. Can anyone tell me what might be going on here? I've seen this in the wild on other sites, too -- also intermittent, not generally occurring but annoying when it happens.

推荐答案

你有一个使用store_location或类似的功能,将用户重定向到一个特定的网页,他们已经登录后任何身份验证code?

Do you have any authentication code that uses store_location or similar functionality to redirect the user to a specific page after they have logged in?

我曾经有过类似的问题,即AJAX回调是导致store_location存储JSON URL,用户被重定向到JSON响应,而不是自己的仪表板。这是很难追查,因为用户不得不闲置在页面前一段时间的错误是明显的。

I once had a similar problem where the AJAX callback was causing store_location to store the JSON URL and the user was redirected to the JSON response rather than their dashboard. This was hard to track down because the user had to sit idle at the page for some time before the bug was manifest.

在任何情况下,我怀疑它是这样的,而不是使用Rails的一个问题。

In any case, I suspect it is something like this rather than a problem with Rails.

这篇关于浏览器有时会显示所需的页面的JSON文本,而不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆