简体   繁体   English

向AJAX成功函数发送多个参数

[英]Sending Multiple Arguments to AJAX Success Function

I am currently having trouble with sending multiple arguments to a callback success function related to AJAX. 我目前无法将多个参数发送给与AJAX相关的回调成功函数。

All I am pretty much looking to do is preserve the value of a variable that came in as a parameter to the function that the AJAX call is enclosed in. 我几乎要做的就是保留一个变量的值,该变量作为AJAX调用所包含函数的参数来输入。

I can get as far as the value being present in the native success function, but once I try to pass it and retrieve it in my call back method, all of a sudden the variable is undefined. 我可以得到本机成功函数中存在的值,但是一旦我尝试传递它并在我的回调方法中检索它,就突然使该变量不确定。 I am confused as to why this is happening and thought the value would send to the function without an issue. 我对为什么会发生这种情况感到困惑,并认为该值可以毫无问题地发送到函数。

If anyone could shed some light on this I would greatly appreciate it. 如果有人能对此有所了解,我将不胜感激。 My thinking says this it probably has something to do with scope, $this, and how I'm passing my variable, but I haven't been able to put it all together yet. 我的想法是,这可能与范围,$ this以及我如何传递变量有关,但是我还无法将它们放在一起。 Below is my code: 下面是我的代码:

function getNum(numb, parentWindow)
{

alert(parentWindow);

alert("before ajax");
$.support.cors = true;

            $.ajax
            ({
               type: "POST",
               crossDomain: true,
               beforeSend: function(request)
               {
                   request.setRequestHeader("Authorization", "Basic");
                   return true;
               },
           data: {
              'parentWindow' :parentWindow
           },
               url: webServiceURL,
               dataType: "xml",
           context: parentWindow,
               async: false,
               data: soapMessage, 
               contentType: "text/xml; charset=\"utf-8\"",
               success: function(data)
               {
                   alert(this);
                   alert(parentWindow);
                   OnSuccess(data, parentWindow);
               },          
               error: OnError
});     
}

The alerting of parentWindow right above, does come back as being an object. 右上方的parentWindow警报确实作为对象返回。 The second part is below for the OnSuccess function that is being called. 下面的第二部分是正在调用的OnSuccess函数。

function OnSuccess(data, status, parentWindow)
    {

       alert("my window " + data.parentWindow);
       alert("my window " + this.parentWindow);

       var documentValArr = parseXMLRrec(data);

        if(documentValArr)
        {

        insertDataIntoTable(documentValArr);

        }

    }

The alerts in the OnSuccess function both return the parentWindow parameter as undefined and I'm just having a confusing time trying to figure out why this is. OnSuccess函数中的警报都将未定义的parentWindow参数返回,而我正试图弄清楚为什么会出现混乱的时间。 If anyone could give me a pointer or two I would be very thankful! 如果有人可以给我一个或两个指针,我将非常感激!

function OnSuccess(data, status, parentWindow)

this function takes 3 parameters. 此功能需要3个参数。

OnSuccess(data, parentWindow);

You only send 2 parameters in. As it is now, your function receives parentWindow as status . 您只发送2个参数。到目前为止,您的函数将parentWindow接收为status

Assuming that the Ajax success callback has the data that you want (as you say: it has), then it is not a closure problem. 假设Ajax success回调具有所需的数据(如您所说:具有),那么这不是关闭问题。

Check your arguments and parameters order 检查您的参数和参数顺序

success: function(data) { alert(this); alert(parentWindow); OnSuccess(data, parentWindow); }, 

Should be 应该

success: function(data) { alert(this); alert(parentWindow); OnSuccess(data,'', parentWindow); }, 

In onSuccess function access the parentWindow without 'this' or 'data' 在onSuccess函数中,访问不带“ this”或“ data”的parentWindow

alert("my window " + parentWindow);

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

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