我目前正在创建一个网站。我遇到的问题是我无法使用 Javascript 重定向到另一个 ASPX 页面。这是我的代码:
<asp:Button ID="Save" runat="server" Text="Schedule" CssClass="vs-btn mT10" OnClientClick="return SendFormValuesForXml(); return false;" />
上面的 Javascript 函数执行 AJAX 调用,成功后我会重定向到另一个正在执行多项操作的页面。 AJAX 调用在这里:
var 响应字符串 = "";
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/MakeXml",
dataType: "json",
data: "{'data':'" + stringToSend + "'}",
async: false,
cache: false,
success: function (result) {
debugger;
window.location.reload(true);
var valuationUrl = window.location.protocol + "//" + window.location.host + "/ValuationList.aspx";
window.location.assign(valuationUrl);
return false;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
}
});
现在,除了成功函数中的重定向之外,一切都正常进行。知道是什么干扰了 javascript 吗? 附加信息:
- No update panel is working on the page, or any other page of the website.
- No error is thrown by the browser except the bad Chrome 'jquery.min.map' not found error.
到目前为止我已经尝试过:
- document.location.replace(valuationUrl);
- location.href = valuationUrl;
- $(location).attr('href', valuationUrl);
堆栈溢出中提到的所有其他选项都是这个 link .
更新:我采用的错误解决方案 在做了很多事情之后,比如使用一个不可见的按钮并在其“点击重定向”页面上运行服务器端功能等,所有这些都失败了。 我所做的是将隐藏字段设置为 0,然后将其值更新为 1。稍后,当加载同一页面时,我检查其值是否为 1,因此强制重定向到下一页。
最佳答案
尝试window.location =valuationUrl;
——如果有效,那么您将遇到安全违规,因为您重定向到的域与重定向的域不同。
Mozilla Developer Network Location.Assign
The Location.assign() method causes the window to load and display the document at the URL specified.
If the assignment can't happen because of a security violation, a DOMException of the SECURITY_ERROR type is thrown. This happens if the origin of the script calling the method is different from the origin of the page originally described by the Location object, mostly when the script is hosted on a different domain.
Mozilla Developer Network Location.Replace
The Location.replace() method replaces the current resource with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session History, meaning the user won't be able to use the back button to navigate to it.
If the assignment can't happen because of a security violation, a DOMException of the SECURITY_ERROR type is thrown. This happens if the origin of the script calling the method is different from the origin of the page originally described by the Location object, mostly when the script is hosted on a different domain.
关于Javascript 重定向无法正常工作 ASP.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25649684/