重定向发出PATCH请求而不是GET

编程入门 行业动态 更新时间:2024-10-24 21:36:55
本文介绍了重定向发出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

本文发布于:2023-10-10 22:39:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479956.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   重定向   PATCH

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!