简体   繁体   English

Javascript AJAX 请求问题

[英]Javascript AJAX request problems

I'm having a problem executing a certain bit of AJAX.我在执行 AJAX 的某个位时遇到问题。 Here is how I want to happen:这是我想要发生的方式:

I have a form where upon pressing a button I want to execute a small bit of Javascript (confirm the execution of the action), send an AJAX request, and then submit the form.我有一个表单,在按下按钮时,我想执行一小部分 Javascript(确认操作的执行),发送 AJAX 请求,然后提交表单。 I don't care about the output of the AJAX request, but it needs to run before the form is submitted.我不关心 AJAX 请求的 output ,但它需要在提交表单之前运行。

The button is defined as:按钮定义为:

<input title="Delete" onclick="return check_delete('id');" 
type="submit" name="Delete" value="Delete">

The function check_delete is: function check_delete 是:

var sure = confirm('Are you sure you want to delete this?');
if (!sure) return false;

var del = confirm('Would you like to delete related content?');
if (window.XMLHttpRequest) {
    xmlhttp=new XMLHttpRequest();
} else {
    xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}
var url = '/del_check.php&id='+id+'&del='+del;

xmlhttp.open('GET',url,true);
xmlhttp.send();
return true;

The function runs fine, except for the xmlhttp.send() part - the request does not show up in the web-server's logs, and does not get executed. function 运行良好,除了xmlhttp.send()部分 - 请求不会显示在 Web 服务器的日志中,也不会被执行。 After playing with it a bit, I found that if I change the last line in the function to return false instead of true , my AJAX starts working, however, because of the false return value, the form does not get submitted.玩了一下之后,我发现如果我将 function 中的最后一行更改为返回false而不是true ,我的 AJAX 开始工作,但是,由于返回值为false ,表单没有被提交。

What am I missing?我错过了什么?

This line:这一行:

xmlhttp.open('GET',url,true);

means you're issuing an asynchronous request ( MSDN , W3C ).表示您正在发出异步请求( MSDNW3C )。 The request is initiated by your code, but your code doesn't block waiting for it to complete (which is normally a Good Thing tm ).该请求由您的代码发起,但您的代码不会阻止等待它完成(这通常是一件好事tm )。

However, when you submit a form, the page is torn down and replaced with the result of the form submission.但是,当您提交表单时,该页面将被拆除并替换为表单提交的结果。 Any outstanding or queued ajax requests will be aborted or never even started.任何未完成或排队的 ajax 请求都将被中止,甚至永远不会启动。

You could make the request synchronous (blocking) by changing that true to a false , but then you'll lock up the page (at best) or the browser (at worst) until that request completes, which isn't ideal.可以通过将true更改为false来使请求同步(阻塞),但随后您将锁定页面(最好的情况)或浏览器(最坏的情况),直到该请求完成,这并不理想。

Alternatives are to avoid doing the ajax request entirely, or to cancel the form submission, wait for the ajax response (even though you don't care about it), and then submit the form programmatically (via HTMLFormElement#submit — eg, calling submit on the form 's DOM element).替代方法是避免完全执行 ajax 请求,或者取消表单提交,等待 ajax 响应(即使您不关心它),然后以编程方式提交表单(通过HTMLFormElement#submit — 例如,调用submitform的 DOM 元素上)。 If you do that second one, note that the button you clicked won't be included in the form data unless you manually insert it, since the form is no longer being submitted by that button.如果您执行第二个,请注意您单击的按钮不会包含在表单数据中,除非您手动插入它,因为该按钮不再提交表单。

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

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