React to 303 status code in jquery ( Prevent from redirecting)

一曲冷凌霜 提交于 2019-12-12 07:26:26

问题


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.

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.

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).

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.

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?


回答1:


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.

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




回答2:


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.

$.ajax({
  url: "example.html",
  cache: false
  }
}).done(function( html ) {
  $("#results").append(html);
});



回答3:


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.




回答4:


See statusCode property of your .ajax() call

Example :

$.ajax({
  statusCode: {
    303: function() {
      alert("page not found");
    }
  }
});


来源:https://stackoverflow.com/questions/16337697/react-to-303-status-code-in-jquery-prevent-from-redirecting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!