简体   繁体   English

jQuery将参数传递到.ajax()

[英]jQuery passing parameters into .ajax()

I'm having issues passing parameters/variables into the ajax() function. 我在将参数/变量传递给ajax()函数时遇到问题。

The function below accepts two parameters. 下面的函数接受两个参数。 One is 'element', the success option of .ajax() uses, that works fine so no need to focus on that. 一种是'element',它是.ajax()的成功选项,可以很好地工作,因此无需专心。 The 'tx' is either a single value like 'menu' or a couple of values separated by a colon like this: "menu:categories:brands" “ tx”可以是单个值(如“菜单”),也可以是由冒号分隔的几个值,例如:“ menu:categories:brands”

If there is only one value in 'tx' then only one AJAX request needs to be sent and that works fine. 如果“ tx”中只有一个值,则仅需要发送一个AJAX请求,这样就可以正常工作。 If there is more than one value in 'tx' the function split's it using ":" is the delimiter and then passes offset[0] as the value of 'tx' into the AJAX request and then store's the rest of the values in 'x'. 如果'tx'中有多个值,则使用“:”作为分隔符将其拆分,然后将offset [0]作为'tx'的值传递到AJAX请求中,然后将其余的值存储在' X'。

What I'm having issues with is running this function recursively once the AJAX request is complete for the first value. 一旦第一个值的AJAX请求完成,我遇到的问题就是递归运行此功能。 The var 'more' stores a bool value if there is more values left to process or not. 如果还有更多的值需要处理,则var'more'存储布尔值。 However, when I write in an if argument using 'more == true' into the success or complete option of .ajax() it doesn't reflect the value stored in that variables, it always returns false. 但是,当我在.ajax()的成功或完全选项中使用'more == true'将if参数写入时,它不能反映存储在该变量中的值,因此始终返回false。

Before some one answers, with "You should structure the function that calls this function to only pass a single value into that parameter," lets suppose that this is an impossible venture that is outside the realm of my control. 在给出一个答案之前,使用“您应该构造调用该函数的函数以仅将单个值传递给该参数”,让我们假设这是我控制范围之外的不可能的尝试。

I have no idea why this happening. 我不知道为什么会这样。 I know it's probably something very simple that I'm overlooking but I've been up for about 16 hours now and this issue has plagued me for at least half that time. 我知道我可能忽略了很简单的事情,但是我已经工作了大约16个小时,这个问题困扰了我至少一半的时间。

Any help here is appreciated. 感谢您的帮助。 Here is the function: 这是函数:

function getContent(element, tx) {
    e = element
    modAmount = tx.split(':')
    if (modAmount.length > 1) {
        x = ''
        tx = modAmount[0]
        for (i=1;i<modAmount.length;i++) {
            x = x + modAmount[i]
            if (i != (modAmount.length)-1){
                x = x+":"
            }
        }
        more = true
    }
    else {
        more = false
        tx = modAmount[0]
    }
    $.ajax({  
        type: "POST",  
        url: "getModule.php",  
        data: "modName="+tx+"&DB=<?php echo DB ?>",  
        success: function(data){  
            if ($( element ).find('p').text() == "No Content"){
                $( element ).find('p').remove();
                $( element ).html("<div onclick='destroy(this)' id='destroy' class='ui-corner-all destroy' title='Remove Module'>"+data+"</div>")
            }
            else {
                $( element ).append("<div onclick='destroy(this)' id='destroy' class='ui-corner-all destroy' title='Remove Module'>"+data+"</div>");
            }
        },
        complete: function() {
            if (more == true) {alert(x)} // always returns false
        }
    });

}

There's already one glaring error here: you're not declaring your local variables using the var keyword. 这里已经存在一个明显的错误:您没有使用var关键字声明局部变量。 This means that there's only one instance of any of those variables, in the global context. 这意味着在全局上下文中,这些变量中只有一个实例。

I'm not sure if this is your problem, but it certainly seems like it could be. 我不确定这是否是您的问题,但似乎确实可以。 Consider the case where you call getContent "foo:bar". 考虑将getContent称为“ foo:bar”的情况。 The first call sets more to true , but then the second recursive call sets more back to false . 第一个调用将更多设置为true ,但是第二个递归调用将更多设置为false When the first XmlHttpRequest finishes, the completion callback will see the value of false rather than the true you were expecting, since those two calls are sharing the same variable for more . 当第一个XmlHttpRequest完成时,完成回调将看到false值,而不是您期望的true值,因为这两个调用共享相同的变量more

You should read up on how JavaScript/ECMAScript scoping works, and gain a solid understanding of how closures work in the language. 您应该阅读JavaScript / ECMAScript范围定义的工作原理,并且对闭包在语言中的工作原理有深入的了解。 This will prevent a lot of head-scratching in the future. 这将防止将来出现很多麻烦。 Google around for writings by Douglas Crockford; 谷歌到处寻找道格拉斯·克罗克福德的著作; that's a good starting point. 这是一个很好的起点。

tl;dr: define your local variables using the var keyword. tl; dr:使用var关键字定义局部变量。

Because "more" isn't scoped within your callbacks. 因为“更多”不在您的回调之内。

I recommend using the context property you can send to the ajax call to set the "this" variable in success/complete callbacks: 我建议使用可以发送给ajax调用的context属性,以在成功/完成回调中设置“ this”变量:

http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

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

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