作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用不引人注目的 JS,在我的 Ruby On Rails 应用程序中使用 JQuery。
用户填写表单后,客户端 JQuery 代码调用:
$.post("/premises", ui.form)
我可以看到 POST 命中了服务器,并且我可以看到服务器向 http://localhost:3000/users/42 发出了重定向通知完成要显示的数据。
但是浏览器页面没有变化。这并没有让我感到惊讶——客户端 javascript 的全部意义在于控制更新的内容——我明白了。但在这种情况下,我想尊重服务器回复的任何内容。
我尝试根据 How to manage a redirect request after a jQuery Ajax call 将调用扩展到 post() :
$.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 未定义。我怀疑真正的答案很简单,对吧?敬请期待!
最佳答案
您提到的帖子使用 JSON 作为返回值,它正在服务器端构建该 json。这意味着如果有重定向,您的数据对象将看起来像
{redirect:'redirecturl.html'}
如果它不是重定向那么数据对象就像
{form:html-string-for-form}
现在的工作是在服务器端相应地构造json对象
关于javascript - jquery.post() : how do i honor a redirect from the server?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5654950/
我是一名优秀的程序员,十分优秀!