javascript 对 jquery 中的 303 状态代码做出反应(防止重定向)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16337697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
React to 303 status code in jquery ( Prevent from redirecting)
提问by keinabel
when I send requests to a certain server, a 303 response will come, followed by the requested response in combination with a 200 status code. Funny thing is that I only see this on my developer console's network view. When checking the statuscode and response of my $.ajax() request, there will be the response of the second request, as well as a 200 http status code.
当我向某个服务器发送请求时,会出现 303 响应,然后是请求的响应和 200 状态代码。有趣的是,我只在我的开发者控制台的网络视图中看到了这一点。检查我的$.ajax()请求的statuscode和response时,会有第二个请求的response,还有一个200的http状态码。
The problem is that it seems that the second request is being cached (though 200 status code), and I really need it to be non-cachable. Therefore I'd really like to intervene into the forwarding process that occurs with a http 303 status code. I'd like my jquery function to check for the status code, then send the get request with explicit headers, that tell the server not to cache the response.
问题是第二个请求似乎正在被缓存(尽管 200 状态代码),我真的需要它是不可缓存的。因此,我真的很想干预 http 303 状态代码发生的转发过程。我希望我的 jquery 函数检查状态代码,然后发送带有显式标头的 get 请求,告诉服务器不要缓存响应。
Well, I just don't know how to do this, since (as mentioned above) the jQuery.ajax method will respond with the forwarded request's response and status code (200).
好吧,我只是不知道该怎么做,因为(如上所述)jQuery.ajax 方法将使用转发请求的响应和状态代码 (200) 进行响应。
Can you help me?
你能帮助我吗?
edit
编辑
10.3.4 303 See Other
The response to the request can be found under a different URI and SHOULD be retrieved using a GET method on that resource. This method exists primarily to allow the output of a POST-activated script to redirect the user agent to a selected resource. The new URI is not a substitute reference for the originally requested resource. The 303 response MUST NOT be cached, but the response to the second (redirected) request might be cacheable.
10.3.4 303 见其他
对请求的响应可以在不同的 URI 下找到,并且应该使用该资源的 GET 方法检索。此方法的存在主要是为了允许 POST 激活脚本的输出将用户代理重定向到选定的资源。新的 URI 不是原始请求资源的替代引用。303 响应不能被缓存,但对第二个(重定向)请求的响应可能是可缓存的。
maybe I need to somehow prevent the user agent from redirecting himself, so I can do the redirect?
也许我需要以某种方式阻止用户代理重定向自己,以便我可以进行重定向?
Or is there a way to simply tell the server/browser not to cache the second request from client-side? or to prevent it from redirecting my request? or at least modify the second request before redirecting?
或者有没有办法简单地告诉服务器/浏览器不要缓存来自客户端的第二个请求?或者阻止它重定向我的请求?或者至少在重定向之前修改第二个请求?
采纳答案by badunk
Responses in the 300 range are meant to be transparent. AFAIK, web browsers don't expose any of them to javascript. Thus, handling the 303 is not an option.
300 范围内的响应是透明的。AFAIK,网络浏览器不会将它们中的任何一个暴露给 javascript。因此,处理 303 不是一种选择。
Have you tried setting the cache
property to false in the ajaxSetup? It will append a timestamp to the request, preventing the browser from caching the response. You can also do that manually yourself. The browser should match the first request url to the 2nd response
您是否尝试cache
在 ajaxSetup 中将该属性设置为 false?它将在请求中附加一个时间戳,防止浏览器缓存响应。您也可以自己手动执行此操作。浏览器应该将第一个请求 url 与第二个响应匹配
回答by Old Pro
You cannot stop the browser from following the 303 redirect. It sounds like that's not what you want, anyway. Whatever you would do in the browser to prevent the original request from being cached should work equally well for preventing the redirected 200 from being cached. That said, there is little you can do on the browser side other than using a unique URL for each request. This is done for you automatically by jQuery when you set cache: false
.
您无法阻止浏览器跟随 303 重定向。无论如何,这听起来不是你想要的。无论您在浏览器中做什么来防止原始请求被缓存,都应该同样适用于防止重定向的 200 被缓存。也就是说,除了为每个请求使用唯一的 URL 之外,您在浏览器端几乎无能为力。当您设置cache: false
.
$.ajax({
url: "example.html",
cache: false
}
}).done(function( html ) {
$("#results").append(html);
});
回答by Brane
This is old post, but maybe someone will find this useful.
这是旧帖子,但也许有人会发现这很有用。
I have the same issue, and in my case the problem was in the method that was called out using the ajax. The problem was in the redirection that was set in the method. So, based on this, you can't use both ajax and redirect(server side), and i removed redirect()
function from method, i everything works as expected.
Btw, i am using codeigniter
.
我有同样的问题,就我而言,问题出在使用 ajax 调用的方法中。问题出在方法中设置的重定向中。因此,基于此,您不能同时使用 ajax 和重定向(服务器端),并且我redirect()
从方法中删除了函数,我一切都按预期工作。顺便说一句,我正在使用codeigniter
.
回答by Nick Andriopoulos
See statusCode
property of your .ajax()
call
查看statusCode
您.ajax()
通话的属性
Example :
例子 :
$.ajax({
statusCode: {
303: function() {
alert("page not found");
}
}
});