jquery.post():我如何遵守来自服务器的重定向?

发布于 2024-11-01 20:15:27 字数 1053 浏览 7 评论 0原文

我正在尝试不引人注目的 JS,在我的 Ruby On Rails 应用程序中使用 JQuery。

用户填写表单后,客户端 JQuery 代码调用:

$.post("/premises", ui.form)

我可以看到 POST 到达服务器,并且可以看到服务器发出重定向通知到 http://localhost:3000/users/42 填写要显示的数据。

但浏览器页面没有改变。这并不让我感到惊讶——客户端 JavaScript 的全部意义就是控制更新的内容——我明白了。但在这种情况下,我想尊重服务器回复的任何内容。

我尝试根据 如何在 jQuery Ajax 调用后管理重定向请求

$.post("/premises", 
       ui.item,  
       function(data, textStatus) {
           if (data.redirect) {
               // data.redirect contains the string URL to redirect to
               window.location.href = data.redirect;
           } else {
               // data.form contains the HTML for the replacement form
               $("#myform").replaceWith(data.form);
           }
       });

...但是(除其他问题外)data.redirect 未定义。我怀疑真正的答案很简单,对吧?期待!

I'm trying my hand at unobtrusive JS, using JQuery in my Ruby On Rails app.

After the user fills out a form, the client-side JQuery code calls:

$.post("/premises", ui.form)

I can see the POST hit the server, and I can see the server emit a redirect notice to http://localhost:3000/users/42 complete with the data to be displayed.

But the browser page doesn't change. This doesn't really surprise me -- the whole point of client-side javascript is to control what gets updated -- I get that. But in this case, I'd like to honor whatever the server replies with.

I tried extending the call to post() based on How to manage a redirect request after a jQuery Ajax call:

$.post("/premises", 
       ui.item,  
       function(data, textStatus) {
           if (data.redirect) {
               // data.redirect contains the string URL to redirect to
               window.location.href = data.redirect;
           } else {
               // data.form contains the HTML for the replacement form
               $("#myform").replaceWith(data.form);
           }
       });

... but (among other problems) data.redirect is undefined. I suspect the real answer is simple, right? Looking forward to it!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧情勿念 2024-11-08 20:15:27

您提到的帖子使用 JSON 作为返回值,并且它正在服务器端构建该 json。这意味着如果有重定向,您的数据对象将如下所示

{redirect:'redirecturl.html'}

,如果没有重定向,则数据对象将如下所示,

{form:html-string-for-form}

现在的工作是在服务器端相应地构造 json 对象

The post you refer to uses JSON as return value and it is constructing that json on server side. it means if there is redirect your data object would look like

{redirect:'redirecturl.html'}

and if it is not redirect then data object would be like

{form:html-string-for-form}

now job is to construct json object accordingly on server side

人疚 2024-11-08 20:15:27

服务器表示您想要使用 JavaScript 处理的数据可以在不同的 URL 上获得,而不是浏览器应该将新文档加载到顶级框架中。将浏览器发送到被告知使用 JS 请求数据的 URL 不会遵循重定向。

如果您想这样做,那么服务器应该响应数据(在响应正文中),JavaScript 将这些数据解释为向 location 分配新值的原因。

The server is saying that the data you want to process with JavaScript is available at a different URL, not that the browser should load a new document into the top level frame. Sending the browser to the URL where it was told the data it was requesting with JS is wouldn't be honouring the redirect.

If you want to do that, then the server should respond with data (in the body of the response) that the JavaScript interprets as a reason to assign a new value to location.

时光沙漏 2024-11-08 20:15:27

data.redirect 可能未定义,因为您没有在服务器端指定它。在您链接到的答案中,无论结果如何,服务器始终以 200 响应,然后它发送回的 JSON 正文决定客户端的反应。因此,在服务器端,您需要使用 {"redirect" : "/where/to/go"} 进行响应

data.redirect is probably undefined because you're not specifying it on the server side. In the answer you linked to the point was to have the server always respond with 200 regardless of the outcome, and then the JSON body it sends back determines how the client reacts. So, on the server side you'd want to respond with {"redirect" : "/where/to/go"}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文