简体   繁体   English

JavaScript无法在Firefox中发送发布参数

[英]Javascript not sending post parameters in Firefox

I have the following js function that sends a post request back to the main index page. 我有以下js函数,可将发布请求发送回主索引页面。 This script is working in Chrome but is not sending any parameters in firefox while still posting back to the page. 该脚本可在Chrome中运行,但在发回页面时仍无法在firefox中发送任何参数。 I am verifying this via the network tab after inspecting the page in firefox. 在检查Firefox中的页面后,我将通过“网络”标签进行验证。

function addDrive(ans){
    $.post("index.php", {add_drive: ans });
    location.reload();
}

This function is called via a onClick on a button I have placed on my page: 通过我在页面上放置的按钮上的onClick调用此函数:

<button onClick="addDrive('y')">Add Drive</button>

I have used similar functions in the past that have worked for both firefox and chrome and I just cant figure out why this does not work in firefox. 过去我曾经使用过适用于Firefox和chrome的类似功能,但我无法弄清楚为什么它在Firefox中不起作用。

My suggestion would be doing something like this : 我的建议是做这样的事情:

function addDrive(ans) {
  $.post("index.php", {add_drive: ans}).then(function() {
    location.reload();
  });
}

This would ensure that you reload the page only after successfully finishing the post request. 这样可以确保仅在成功完成发布请求后才重新加载页面。 You could also provide a success handler instead if you prefer that instead of using this promises API. 如果您愿意,也可以提供success处理程序,而不是使用此promises API。 You could also do the same in an always handler to ensure it reloads the page even if the request fails but that would be subjective to your requirements. 你也可以做同样的在always处理程序,以确保即使请求失败,但是这将是主观您的要求它重新加载页面。

However, I would argue that this doesn't look like good practice at all, if you have to reload the page you could just have a form and post that instead of trying to post using the jquery handler. 但是,我认为这根本不是一种好的做法,如果您必须重新加载页面,则可以只拥有一个表单并将其发布,而不是尝试使用jquery处理程序进行发布。 You could do something like form.submit() in javascript and that would submit the form and it would submit the data in the form by a post request as long as the method on the form is set to 'POST', that way you don't have to reload the page manually and you can do that from the server end. 您可以在javascript中执行诸如form.submit() ,并且只要表单上的方法设置为“ POST”,就可以通过邮寄请求提交表单并通过表单提交数据。您不必手动重新加载页面,您可以从服务器端进行重新加载。

Try to reload your page after successful post: 成功发布后,尝试重新加载页面:

$.post("index.php", {add_drive: ans}).done(function( data ) {
    location.reload();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM