在不知道是哪个的情况下停止 ajax 请求
我正在使用 primefaces 的自动完成组件。现在,您添加一些字符,它就会相应地搜索名称。现在我遇到的麻烦是“输入”按钮(键盘上)...因为我想让输入功能起作用,即使您正在搜索自动完成功能,您仍然可以按下它并继续获得更大的列表。现在,这是通过一个按钮完成的,该按钮转到 Webflow 并重定向到另一个页面。
现在我遇到的麻烦是,如果自动完成开始搜索,它会启动一个 Ajax 请求,如果我在此期间单击 Enter 键,则会启动另一个请求来执行重定向。但这里有一个问题:第一个请求仍然处于活动状态,如果它在我的重定向完成之前返回成功,它只会中止重定向,给我它开始的页面。
现在的大问题是:如何停止我一无所知的第一个请求?黑盒方面我对这个请求不太了解,只是它非常烦人。我也无法通过 JavaScript 传递它,因为自动完成功能只有一个在请求之前加载的启动函数和在请求之后加载的完成加载函数。
编辑:这是我使用的代码,以防您需要它来理解:
XHTML 和 JavaScript:
<p:autoComplete value="#{simpleSearchBean.searchString}" completeMethod="# {simpleSearchBean.completeUser}"
var="user" itemLabel="#{user}" itemValue="#{user}" forceSelection="false"
minQueryLength="3" maxResults="10" name="search_block_form" id="edit-search-block-form-1" size="30" queryDelay="500" style="float:left;">
<p:ajax event="itemSelect" oncomplete="autoClickSearchButton()" update="@all"/>
</p:autoComplete>
<p:commandButton action="simpleUserSearch" title="#{msgs_common['common.search']}" name="op" id="edit-submit" image="ui-icon ui-icon-search" style="float:left;"/>
<script type="text/javascript">
function autoClickSearchButton(){
jQuery('#search-block-form\\:edit-submit').click();
}
jQuery('#search-block-form\\:edit-search-block-form-1').keyup(function(event){
if(event.keyCode == 13){
jQuery('#search-block-form\\:edit-submit').click();
}
});
</script>
这是 webflow 部分:
<end-state id="simpleUserSearch" view="flowRedirect:user?searchString=#{simpleSearchBean.searchString}"/>
<global-transitions>
<transition on="simpleUserSearch" to="simpleUserSearch" validate="false"/>
</global-transitions>
I'm using the autocomplete component of primefaces. Now what this does is you add a few characters and it will search the names accordingly. Now the trouble I am having is the 'enter' button (on keyboard)... Because I want to make the enter functional, that even if you are searching the autocomplete you can still press it and continue to get a larger list. Now this is done with a button that goes to Webflow and redirects to another page.
Now the trouble I am having is, that if the autocomplete starts searching, it starts an Ajax request and if I click the enter during it will start another request to do the redirect. But here is the catch: The first request is still active and if it returns success before my redirect is complete it just aborts the redirect giving me the page where it began.
Now the big question: How do I stop the first request I know nothing about? Blackbox wise I don't know much about the request except that it is very annoying. I cannot pass it on via JavaScript either because the autocomplete only has a function for on start which loads before the request and on complete which loads after the request.
Edit: Here is the code I use in case you need it to understand:
XHTML and JavaScript:
<p:autoComplete value="#{simpleSearchBean.searchString}" completeMethod="# {simpleSearchBean.completeUser}"
var="user" itemLabel="#{user}" itemValue="#{user}" forceSelection="false"
minQueryLength="3" maxResults="10" name="search_block_form" id="edit-search-block-form-1" size="30" queryDelay="500" style="float:left;">
<p:ajax event="itemSelect" oncomplete="autoClickSearchButton()" update="@all"/>
</p:autoComplete>
<p:commandButton action="simpleUserSearch" title="#{msgs_common['common.search']}" name="op" id="edit-submit" image="ui-icon ui-icon-search" style="float:left;"/>
<script type="text/javascript">
function autoClickSearchButton(){
jQuery('#search-block-form\\:edit-submit').click();
}
jQuery('#search-block-form\\:edit-search-block-form-1').keyup(function(event){
if(event.keyCode == 13){
jQuery('#search-block-form\\:edit-submit').click();
}
});
</script>
And here is the webflow piece:
<end-state id="simpleUserSearch" view="flowRedirect:user?searchString=#{simpleSearchBean.searchString}"/>
<global-transitions>
<transition on="simpleUserSearch" to="simpleUserSearch" validate="false"/>
</global-transitions>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,解决这个问题的最佳方法是这样的:
在提交时执行以下操作:
这将停止运行 ajax 脚本。
I had the same problem the best way to resolve this would be something like this:
on submit do this:
This would stop your running ajax script.
我认为您甚至不需要一个函数来监听用户是否按 Enter 键...自动完成功能已经做到了...如果您希望
search commandButton
重定向到另一个页面,只需将ajax 属性设置为 false
:I don't think you even need a function for listening if the user presses
enter
... the autocomplete already does that... If you want thesearch commandButton
to redirect to another page, just set theajax attribute to false
: