Chrome doesn't seem to fire javascript xmlhttprequests after a form submit, but FF and IE do

倖福魔咒の 提交于 2019-12-13 00:19:05

问题


I use this little trick to update the current page while the next page is loading. When the user clicks submit, I set an interval timer which does xhr calls to the server to find out how much longer the response is going to take and update the page with this information, thus successfully entertaining the user.

In Firefox and IE this works fine, but it seems in chrome after the form submit happens, the setinterval call will still fire but the xmlhttprequest never actually performs the request. I don't get any errors or exceptions, it just does this

    xmlhttp.onreadystatechange = statstatechange;
    xmlhttp.open("POST", url, true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.send(postdata);

and my statstatechange function never gets called, and I see no requests on the server side, so as far as I can tell the request never gets made.

Any idea how to make chrome do this?


回答1:


For anybody having the same problem in chrome: it turns out that if you have an iframe on the page, you can fire xhr requests in that iframe and they will continue to work even after the main top level page form has been submitted. So it's a crappy workaround but at least it functions.




回答2:


try adding these two

xmlhttp.setRequestHeader('Content-length',url.length);
xmlhttp.setRequestHeader('Connection','close');


来源:https://stackoverflow.com/questions/8057662/chrome-doesnt-seem-to-fire-javascript-xmlhttprequests-after-a-form-submit-but

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!