Rails 控制器处理为 */* [英] Rails Controller processing as */*

查看:45
本文介绍了Rails 控制器处理为 */*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 js.erb 文件,我在其中执行 ajax GET,如下所示:

I have a js.erb file where I perform an ajax GET like so:

$.get("<%= j @stored_location %>");

一切都很好,但是我的服务器日志显示:

All is well, however my server logs show this:

Processing by XyzController#index as */*

我知道/"表示任何格式",并且一切正常,所以我想知道是否有任何意义尝试确保 Rails 将这个操作明确地处理为 <代码>:js.我需要在 $.get 调用中添加一些东西吗?我应该打扰吗?

I know that "/" means "any format", and everything is working just fine, so I'm wondering if there's any point in trying to ensure that Rails processes this action explicitly as :js. Do I need to add something to the $.get call? Should I bother?

推荐答案

1: 你应该打扰吗?

我认为对这类事情尽可能准确和明确总是一个好习惯.如果您希望能够根据将来的请求时间对相同的 URL 做出不同的回答,您将不得不处理这个问题.所以是的,你应该打扰.更糟糕的是,你会从中学到新的东西:)

I think it is always a good habit to be as exact and explicit as you can with those kind of things. If you would like to be able to answer differently for the same URL depending on the request time in the future you will have to deal with this issue. So yes, you should bother. Worse case, you'll learn something new from this :)

2:如何解决这个问题?

2: How to fix this?

Jquery Get 文档 列出了您可以使用的 dataType 参数传递给您的电话.因此,例如,如果您希望从服务器返回 JSON,您的请求可能如下所示:

The Jquery Get documentation lists a dataType argument you can pass to your call. So, for example, if you are expecting a JSON to return from the server your request might look like this:

$.get({
  url: "<%= j @stored_location %>",
  data: data,
  success: success,
  dataType: "json"
});

查看文档并确定最适合您的情况.

Have a look at the documentation and decide what's best for you case.

这篇关于Rails 控制器处理为 */*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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