简体   繁体   English

Javascript表单提交有时无效

[英]Javascript form submit is not working sometime

I am submitting my form during beforeunload event of javascript. 我在javascript的beforeunload事件期间提交我的表单。 It is behaving in a different manner for all the browsers. 对于所有浏览器,它的行为方式不同。 In chrome 29, if I am closing my browser within 1 minute,then it is hitting the server side action, but if my application is idle for more than 1 minute and then I am closing my browser, then its not hitting the server. 在chrome 29中,如果我在1分钟内关闭浏览器,那么它就是服务器端操作,但是如果我的应用程序闲置超过1分钟然后我关闭了我的浏览器,那么它就不会打到服务器了。 Similarly in IE10 and FF20, I cannot stay idle for more than 5 minutes. 同样在IE10和FF20中,我不能闲置超过5分钟。

<script> 
    function updateServerCMIModel(item) {
        document.forms[0].itemID.value = item;
        formstring = top.window.CPFrame.showCurrentModelState("form");
        document.forms[0].data.value = formstring;
        document.forms[0].nextAction.value = top.window.frames.CPFrame.CPAPI.userEvent;
        document.forms[0].lmsAction.value = "update";
        document.forms[0].submit();
    }
</script>

Is there any way to resolve this issue? 有什么方法可以解决这个问题吗?

It's a timing issue. 这是一个时间问题。 You need a way to delay the actual unloading of the page until the form post succeeds. 您需要一种方法来延迟实际卸载页面,直到表单发布成功。 A very typical hack is to throw up a dialogue to the user after posting the form ("Are you sure you want to leave this page?") and hope the user takes a few seconds before clicking. 一个非常典型的黑客是在发布表单后向用户发出一个对话框(“你确定要离开这个页面吗?”)并希望用户在点击之前花几秒钟。

To do this, simply return a string containing a message of your choice from the event handler, eg 要执行此操作,只需从事件处理程序返回包含您选择的消息的字符串,例如

window.onbeforeunload = function(e) {
  doFormPost();
  return 'Dialog text here.';
};

Now the issue is fixed. 现在问题已解决。 I have achieved it through Synchronous AJAX call. 我通过同步AJAX调用实现了它。

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

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