$.get() 提交页面前遇到麻烦
想象一个里面有一些“商品”的购物篮。 我有一个
元素列表,每个元素都有一个包含数字(金额)的字段。从概念上讲,这就是我想要的:当用户按下按钮时,我循环遍历每个
元素来选择金额。然后我执行 $.Get() 来使用商品 ID + 金额来调用服务器,以检查商店是否有足够的特定商品。服务器回复 True 或 False。 该回复临时存储在 html 字段中。 现在,循环完成后,我检查是否有任何商品有错误回复。 如果是这样,我会突出显示“错误”项目。否则我就简单地提交。好的,问题是我的代码接缝继续经过我的 $.get() 回调函数,因此在 $.get() 实际从服务器接收结果之前评估是否返回 false 的最终检查。 无论如何,这就是我认为正在发生的事情......
现在让我们看一些代码:
var tmp = new Array();
var id = '';
var c = '';
var n = 0;
var z=0;
$('#basket').find('li.list').each(function() {
c = $(this).find('input.fldamount').val(); // this is the amount field
id = $(this).attr('id'); // this is the id no of the item
$.get('./(RPC)?OpenAgent&cmd=movewhcheckamount&unid='+id+'&count='+c, function(data) {
$('#RPCResult').val(data); // i store the returned value in a html field
if ( $('#RPCResult').val() == "true" ) {
tmp.push( id+'|'+c ); // if true is returned, i push the id & amount to an array
} else {
$(this).addClass('red'); // else i tag the item
n=n+1; // and then increment a counter
}
} ); // $('#basket')
var t = window.setTimeout( function() {
if (tmp.length > 0 && n == 0) { // if i got items in the array AND my false counter is zero
$('#SelectedArtikler').val( tmp.join(";") ); // then i store the array as text in a field
document._FlyttArtikel.submit(); // and submit
} else if (n > 0) {
// show a popup
alert("You're trying to move more items than exists...");
} else {
alert("ops, nothing to move..."); // should never end up here...
}
}, 1000);
window.clearTimeout(t);
如您所见,我试图用 setTimeout 来抵消运行在我的回调函数之后的代码,所以我基本上等待一秒钟给服务器时间响应。 我确实在 setTimeout 周围有另一个循环,只要我的 #RPCResult 字段为空,就会继续,但这导致了无限循环。
我的 #RPCResult 字段可见,这样我就可以看到发生了什么,我看到的是弹出窗口“您正在尝试移动更多项目...”,在我在该弹出窗口上按“确定”后,然后是 #RPCResult 字段得到结果 true/false。
我现在可以优化一些代码,但我现在感兴趣的是以适当的方式获取 $.get() 结果。
提前致谢 ;-)
Think a shopping basket with some "goods" in it.
I have a <li>
list of elements and each of them has a field containing a number - the amount.
Conceptually this is what i want: When the user presses a button, i loop through each <li>
element picking the amount. Then i do a $.Get() to call the server with the goods id + the amount to check if the store has enough of the particular item. The server replies either True og False.
This reply is temporary stored in a html field.
Now after the looping is done, i check if there were a False reply on any of the goods.
If so i highlight the "false" items. Or else i simply submit.
OK, the problem is that my code seams to continue past my $.get() call-back function, so the final check to see if any false was returned is evaluated before the $.get() actually receives a result from the server.
Anyway this is what i think is happening...
Now lets look at some code:
var tmp = new Array();
var id = '';
var c = '';
var n = 0;
var z=0;
$('#basket').find('li.list').each(function() {
c = $(this).find('input.fldamount').val(); // this is the amount field
id = $(this).attr('id'); // this is the id no of the item
$.get('./(RPC)?OpenAgent&cmd=movewhcheckamount&unid='+id+'&count='+c, function(data) {
$('#RPCResult').val(data); // i store the returned value in a html field
if ( $('#RPCResult').val() == "true" ) {
tmp.push( id+'|'+c ); // if true is returned, i push the id & amount to an array
} else {
$(this).addClass('red'); // else i tag the item
n=n+1; // and then increment a counter
}
} ); // $('#basket')
var t = window.setTimeout( function() {
if (tmp.length > 0 && n == 0) { // if i got items in the array AND my false counter is zero
$('#SelectedArtikler').val( tmp.join(";") ); // then i store the array as text in a field
document._FlyttArtikel.submit(); // and submit
} else if (n > 0) {
// show a popup
alert("You're trying to move more items than exists...");
} else {
alert("ops, nothing to move..."); // should never end up here...
}
}, 1000);
window.clearTimeout(t);
As you can see, i have tried to counter-act the code running past my call-back function with a setTimeout, so that i basically wait a sec to give the server time to respond.
I did have another loop around the setTimeout the continued as long as my #RPCResult field was empty, but that resulted in an infinite loop.
I have the #RPCResult field visible so i can see what happens and what i see is that the popup "You're trying to move more items..." is shown and RIGTH AFTER i press ok on that popup THEN the #RPCResult field gets the result true/false.
I now some of the code can be optimized but what i'm interested in right now is getting the $.get() result in a proper fashion.
Thanks in advance ;-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将所有“$.get()”调用完成后要运行的代码放在回调例程内部中。这是唯一可以确保您确实收到服务器响应的地方。记录返回的调用数量,当计数器递增到等于项目总数时,您就知道所有响应都可用。
因此:
为您提供项目数量并初始化计数器。在传递给“$.get()”的回调函数内,您可以执行如下操作:
您的代码似乎已经在执行其中的一些工作(您递增的“n”变量)。
现在,我还要指出的是,为每个“项目”执行单独的 HTTP 事务是相当疯狂的。您应该将它们全部捆绑到一个 HTTP 请求中,该请求返回答案列表。
另外,在“RPCresult”字段中存储对“$.get()”的响应实际上没有任何意义;只需检查“data”的值,因为无论如何都不会再查看“RPCresult”。
You're going to have to place the code that's going to run when all the "$.get()" calls are finished inside the callback routine. That's the only place where you can be sure that you've actually gotten the server response(s). Keep a counter of how many calls have returned, and when the counter increments up to be equal to the total number of items, then you know that all of the responses are available.
Thus:
gives you the number of items and initializes the counter. Inside the callback function passed to "$.get()" you can then do something like this:
Your code already appears to be doing some of this work (the "n" variable that you increment).
Now, that said, I'll also note that it's quite crazy to perform separate HTTP transactions for each of your "items". You should bundle them all up into one HTTP request that returns a list of answers.
Also, there's really no point at all in storing the response to the "$.get()" in that "RPCresult" field; just examine the value of "data", since nothing ever looks at "RPCresult" again anyway.