rails如何确定传入请求格式? [英] How does rails determine incoming request format?

查看:186
本文介绍了rails如何确定传入请求格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  respond_to do我想知道rails如何知道请求的格式| format | 
format.html
format.xml
format.json
end

作为一个例子,考虑这种情况我面对了。假设通过javascript(使用jQuery)我做了一个POST请求,说明 dataType:json

  $。 ajax({
type:'POST',
url:'example.com',
data:data,
dataType:'json'
});

当这个请求到达控制器动作时,站在ruby调试器里面,我检查@ request.format我可以看到内容类型是application / json。然后控制器按预期响应json格式。



但我对在路由中指定的格式符号感到困惑。假设向 example.com/parts.json 发出请求,但在请求中,内容类型为application / html或application / xml。 控制器是否响应json格式或html或xml?



谢谢!

解决方案

ActionController :: MimeResponds :Rails从客户端提交的HTTP Accept头中确定所需的响应格式。


I'm just wondering how rails knows the format of the request as to correctly enter in the famous:

respond_to do |format|
  format.html
  format.xml
  format.json
end

As an example consider this situation I have faced up. Suppose that via javascript (using jQuery) I make a POST request expliciting dataType: json

$.ajax({
      type: 'POST',
      url: 'example.com',
      data: data,
      dataType: 'json'
    });

When this request reach controller action, standing inside it with ruby debugger, I inspect @request.format and I can see that content-type is application/json. Then the controller respond to json format as expected.

But I'm confused with the format symbol especified in the routes. Suppose that a request is made to example.com/parts.json but in the request the content type is application/html or application/xml. Is the controller responding to json format or html or xml??

Thanks!

解决方案

From ActionController::MimeResponds: "Rails determines the desired response format from the HTTP Accept header submitted by the client."

这篇关于rails如何确定传入请求格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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