JavaScript以GET请求的形式重新加载页面 [英] JavaScript to reload the page as GET request

查看:52
本文介绍了JavaScript以GET请求的形式重新加载页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看似简单的问题,但找不到答案.我有一个网页,该网页可能是POST请求产生的,并且URL中可能有锚点(#).我想在JavaScript中作为GET请求重新加载此页面.因此类似于此问题,但是我实际上想避免POST,而不仅仅是警告.

I have a seemingly simple question, but can't find the answer. I have a webpage, which may have resulted from a POST request and may have an anchor (#) in the URL. I want to reload this page as a GET request in JavaScript. So it's similar to this question, but I actually want to avoid the POST, not just the warning about it.

例如,如果该页面是由于对" http://server/do/我想重新加载URL" http://server/do/some?thing."作为GET.如果我尝试

So, for example, if the page resulted from a POST request to "http://server/do/some?thing#" I want to reload the URL "http://server/do/some?thing" as a GET. If I try

window.location.reload(true);

导致IE尝试POST.如果我改为这样做:

that causes IE to try a POST. If I instead do:

window.location = window.location.href;

当URL具有锚点时,此操作不执行任何操作.我是否真的需要自己进行字符串操作以摆脱"#whatever",还是有一种更简单,更好"的方式来做到这一点?

this does nothing when the URL has an anchor. Do I really need to do string manipulation myself to get rid of the "#whatever" or is there an easier, "better" way to do this?

推荐答案

到目前为止,我能想到的最好的方法是:

The best I've come up with so far is:

function reloadAsGet()
{
    var loc = window.location;
    window.location = loc.protocol + '//' + loc.host + loc.pathname + loc.search;
}

这篇关于JavaScript以GET请求的形式重新加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆