0

我正在发送一个 jquery $.ajax(); 向服务器发出请求,并希望我的 onsuccess 函数根据我发送到服务器的参数而不是我从服务器收到的参数工作, 因为每次我的站点的行为都应该取决于我发送的内容,而不是我收到的内容我如何附加我的“发送”参数,而不使它们成为我的 onsuccess 函数的全局变量?

这是我的功能:

 $.ajax({
            url : this.soapTarget,
            type : "POST",
            dataType : "xml",
            data : this.soapMSG,
            processData : false,
            beforeSend : function(xhr) {
            xhr.setRequestHeader("SOAPTarget",this.soapTarget);
            xhr.setRequestHeader("SOAPAction",this.soapAction);
            },
            contentType : "text/xml; charset=\"utf-8\"",
            success : OnSuccess,
            error : OnError
            });

当这个函数实际上是一个更全局函数中的参数时,这就是为什么它有 "this.soapMSG" 和 this.soapTarget 和 this.soapAction 作为它的参数

在我的 OnSuccess 函数中,我想使用“this”对象在网页上显示结果

我怎么做?

4

2 回答 2

0

尝试这个,

var self=this;
$.ajax({
    url : this.soapTarget,
    type : "POST",
    dataType : "xml",
    data : this.soapMSG,
    processData : false,
    beforeSend : function(xhr) {
        xhr.setRequestHeader("SOAPTarget",this.soapTarget);
        xhr.setRequestHeader("SOAPAction",this.soapAction);
    },
    contentType : "text/xml; charset=\"utf-8\"",
    success : function(){
        $(self).html('your data goes here'); // self points to this object now
    },
    error : OnError
 });
于 2013-08-10T11:03:38.240 回答
0

需要用您自己的函数包装 onsuccess,如下所示:

succes: function(response, status,xhr){
OnSuccess(response,status,xhr,moreparams);
}
于 2013-08-10T14:02:16.683 回答