开发者

javascript: How to get a XMLHttpRequest representation of the current resource

开发者 https://www.devze.com 2023-03-31 03:03 出处:网络
I want to be able, using javascript, to reload the currently loaded resource, but customising some header variables, before doing so.

I want to be able, using javascript, to reload the currently loaded resource, but customising some header variables, before doing so.

This method is needed/should be used in 2 real cases:

  1. As a way, to handle login via real HTTP-login, which means reloading the resource with attached authentication data
  2. To get different media-type representations of the same resource, which means modifying the accept header, before re-sending the request.

As I would need to leave any other header fields unchanged, as the browser would send it natively, I cannot simply construct my own XMLHttpRequest, using the document.URL value. Rather I would need to get hold of a 开发者_Go百科XMLHttpRequest representation of the request, the browser sent for the current resource.

Is there a way to accomplish that in JS? Preferably using pure JS without any jQuery magic.


If it was loaded via GET, you can just create an XMLHttpRequest and use document.location.href as the URL.

The browser will specify the usual cookie headers, so your request headers should be largely the same unless the GET changed cookies in non-idempotent ways.

If the current resource was loaded via POST, then you are out of luck. The POST body of the current resource is not available to JavaScript so unless you would need some other way to get it or infer it from page content.

0

精彩评论

暂无评论...
验证码 换一张
取 消