jQuery如何?将附加参数传递给$ .ajax调用的成功回调?

dot*_*eek 18 jquery jquery-callback

我似乎是徒劳无功地将其他参数传递回我为成功的ajax调用创建的成功回调方法.一点背景.我有一个页面,其中包含许多动态创建的文本框/选择框对.每对具有动态分配的唯一名称,例如name ="unique-pair-1_txt-url"和name ="unique-pair-1_selectBox",则第二对具有相同但前缀不同.

为了重用代码,我制作了回调来获取数据和对selectbox的引用.但是当回调被触发时,对selectbox的引用将返回为"undefined".我在这里读到它应该是可行的.我甚至尝试过利用'上下文'选项,但仍然没有.这是我尝试使用的脚本块:

<script type="text/javascript" language="javascript">
$j = jQuery.noConflict();
function getImages(urlValue, selectBox) {
    $j.ajax({
        type: "GET",
        url: $j(urlValue).val(),
        dataType: "jsonp",
        context: selectBox,
        success:function(data){
         loadImagesInSelect(data)
        } ,
        error:function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }

    });
}

function loadImagesInSelect(data) {
var select = $j(this);
select.empty();
$j(data).each(function() {
    var theValue = $j(this)[0]["@value"];
    var theId = $j(this)[0]["@name"];
    select.append("<option value='" + theId + "'>" + theValue + "</option>");
});
select.children(":first").attr("selected", true);

}    
</script>
Run Code Online (Sandbox Code Playgroud)

从我所读到的,我觉得我很接近,但我不能把手指放在缺失的环节上.请帮助你典型的忍者隐身方式.TIA

****更新****尼克是一个真正的忍者.他们应该为此发明一个新的徽章!他下面的回答就是诀窍.正如他提到的那样具体是1.4,但我可以忍受.这是我的最终代码,适用于任何Ninjas的培训(以及我未来的参考):

<script type="text/javascript" language="javascript">
$j = jQuery.noConflict();
function getImages(urlValue, selectBox) {
    $j.ajax({
        type: "GET",
        url: urlValue+ '?callback=?',
        dataType: "jsonp",
        context: selectBox,
        success: jQuery.proxy(function (data) {
            var select = $j(this);
            select.empty();
            $j(data).each(function() {
                var theValue = $j(this)[0]["@value"];
                var theId = $j(this)[0]["@name"];
                select.append("<option value='" + theId + "'>" + theValue + "</option>");
            });
            select.children(":first").attr("selected", true);
        }, selectBox),
        error:function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });
}
</script>
Run Code Online (Sandbox Code Playgroud)

Cla*_*oom 22

这就是我做的,它也运行良好:

$.ajax('URL', {
    myCustomControl: selectBox,
    myCustomVariable: 'teste',
    data:
    {
       myData: 1
    },
    success: function (data, textStats, jqXHR) {
        myFunction(data, textStats, jqXHR, this.myCustomControl, this.myCustomVariable);
    }   
});
Run Code Online (Sandbox Code Playgroud)

您可以将控件和变量添加到ajax调用的参数中.

  • 在 chrome 中测试,使用 jquery-1.11.2 似乎,你真的不需要对象:`function ajaxParameterTest(p1, p2) { $.ajax({ url: "/Download.aspx?status=true", success: function (data, status, jqXHR){ callbackFunction(data, status, jqXHR, p1, p2); }, dataType: "html", statusCode:{ 404: function(){ alert("你现在无法从服务器获取数据.."); } } }); }` (2认同)

Ego*_*hin 13

把它放到$ .ajax参数中.

invokedata: {
    data1: "yourdata",
    data2: "moredata"
}
Run Code Online (Sandbox Code Playgroud)

在成功功能中使用它就像这样

this.invokedata.data1;
this.invokedata.data2;
Run Code Online (Sandbox Code Playgroud)

您的$ .ajax调用和成功函数也必须是同一对象的一部分.将您的函数放在一个对象中并像这样定义它们

function myObject {
    var getImage = function(..) { }
    var loadImagesInSelect = function(..) { }
}
Run Code Online (Sandbox Code Playgroud)


Nic*_*ver 12

更新:如果你正在使用jQuery 1.4使用它来简化一些事情:

success: jQuery.proxy(function (data) {
    var select = $j(this);
    select.empty();
    $j(data).each(function() {
        var theValue = $j(this)[0]["@value"];
        var theId = $j(this)[0]["@name"];
        select.append("<option value='" + theId + "'>" + theValue + "</option>");
    });
    select.children(":first").attr("selected", true);
}, selectBox)
Run Code Online (Sandbox Code Playgroud)