后退按钮(Chrome)在Play框架中获取Json而不是HTML [英] Back Button (Chrome) Gets Json instead of HTML in Play Framework

查看:122
本文介绍了后退按钮(Chrome)在Play框架中获取Json而不是HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Folks,
我有一个Web应用程序,我已经在相同资源的JSON和HTML表示中重复使用了相同的路由,现在我们将其称为/ foo / details。这个页面是链接的,我们称之为/ bar / details。 (所以,看看你看到的/ bar / details的链接 - > / foo / details)。

当我从第一页到第二页时,一切正常。当我点击Chrome中的后退按钮时,原始页面会呈现为JSON而非HTML。如果我在浏览器中进行刷新,我会得到HTML表示而不是JSON。



以下是我用来检测JSON与HTML的代码:

  res.result.map {group => 
render {
case Accepts.Html()=> Ok(views.html.groups.details(group))
case Accepts.Json()=> Ok(Json.toJson(group))
}
} .getOrElse(NotFound)

这是这种模式的标准实现,它在任何地方都可以使用,除了在某些情况下在Chrome中使用后退按钮

有没有我没有清除的值,或者我的页面使用Ajax做的事情很混乱使用Json渲染它,或者Chrome浏览器正在缓存页面,但缓存了错误的接受头文件?



我可以通过使用两条不同的路线来解决这个问题,一个用于Json,另一个用于Html,但我不喜欢这种感觉,因为感觉就像我放弃了一样。



有人对后退按钮中的行为有何看法?

解决方案

绝对是Chrome的浏览器缓存。它不会区分使用Accept - >application / json和常规HTML接受标头对/ foo / bar进行的请求。因此,我将加载HTML页面,该页面上的JavaScript会针对原始JSON数据创建相同的URL,然后当我下次回击时,Chrome将提供缓存的JSON而不是缓存的HTML。 / p>

Folks, I've got a web application where I have re-used the same route for the JSON and HTML representations of the same resource, let's call it /foo/details for now. This page is linked from, let's call it /bar/details. (so, looking at /bar/details you see link to -> /foo/details).

When I go from the first page to the second, everything works fine. When I click the back button in Chrome, the original page renders as JSON instead of HTML. If I hit refresh in the browser, I get the HTML representation and not the JSON.

Here's the code I'm using to detect JSON vs HTML:

res.result.map { group =>
  render {
    case Accepts.Html() => Ok(views.html.groups.details(group))
    case Accepts.Json() => Ok(Json.toJson(group))
  }
}.getOrElse(NotFound)

This is the standard implementation of this pattern and it works everywhere, except when I use the back button in Chrome in certain situations.

Is there some value I'm not clearing, or something my pages are doing with Ajax that is confusing Play to make it render in Json, or perhaps Chrome is caching the page but caching the wrong accepts header??

I can get around this by using two different routes, one for Json and one for Html, but I dislike that as it feels like I'm giving up.

Anybody have any ideas as to what causes this behavior in the back button only?

解决方案

It was definitely Chrome's browser cache. It makes no distinction between a request made to /foo/bar with "Accept"->"application/json" and with the regular HTML accept header. As a result, I would load the HTML page, the JavaScript on that page would hit the same URL for raw JSON data, and then the next time I hit back, Chrome would serve up the cached JSON instead of the cached HTML.

As a result, I had to modify my routes so that my JSON/REST API all go through different URLs so that Chrome (and Safari) don't cache the JSON.

这篇关于后退按钮(Chrome)在Play框架中获取Json而不是HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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