重定向发出PATCH请求而不是GET [英] Redirect making a PATCH request instead of GET

查看:63
本文介绍了重定向发出PATCH请求而不是GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从我的application.js文件发出ajax补丁请求:

I'm making an ajax patch request from my application.js file:

$.ajax({
     type: "PATCH",
     url: "/units/" + id,
     data: { 'var_1': var_1,
             'var_2': var_2,},
     success:function(){
            alert("Details saved successfully!!!");
     },
     dataType: "text"
 });

 // stop normal form submission behaviour
 $("#add_form").submit(function(e){ return false; }); 

此请求已由我的控制器成功接收,并且我的对象的属性已更新.然后,我尝试重定向到具有特定ID的索引页.

This request is successfully received by my controller, and the attributes of my object are updated. I then try to redirect to an index page with a particular id.

units_controller.rb

if @unit.update_attributes( ** data ** )
    flash.notice = "Successfully updated "
    redirect_to :controller => "/units", :action => "index", :id => params[:id]
end

我的问题是redirect_to调用包含PATCH方法请求,因此返回路由错误.根据我的routes.rb文件,它应该只是一个GET请求:

My issue is that the redirect_to call contains a PATCH method request and therefore returns a routing error. It should be just a GET request, as per my routes.rb file:

resources :units, except: :show
get '/units/index' => 'units#index'

您知道为什么我的redirect_to调用包含PATCH方法请求以及如何将其更改为GET吗?

Any idea why my redirect_to call contains a PATCH method request and how to change it to a GET?

推荐答案

如果您使用的是GET或POST以外的XHR请求,并在请求之后进行重定向,则某些浏览器将使用原始请求方法来遵循重定向.这可能会导致对于不希望出现的行为,例如双重DELETE.要解决此问题,您可以返回303.请参阅其他状态代码,将使用GET请求.取自 ActionController :: Redirecting#redirect_to 的API文档.
顺便提一句.在这种情况下重定向是不合适的解决方案.您最好应该渲染一个合适的模板,或者只更新资源而不是所有资源(如果可能).

"If you are using XHR requests other than GET or POST and redirecting after the request then some browsers will follow the redirect using the original request method. This may lead to undesirable behavior such as a double DELETE. To work around this you can return a 303. See Other status code which will be followed using a GET request." taken from the API documentation of ActionController::Redirecting#redirect_to.
Btw. redirecting in that situation is not an appropriate solution. You better should render an appropriate template or just only update the resource instead of all resources(if possible).

这篇关于重定向发出PATCH请求而不是GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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