简体   繁体   English

在AJAX调用中使用“上下文:…”的正确方法是什么?

[英]What is the right way to use “context:…” in an AJAX call?

So I'm aware that there are a big amount of threads about AJAX and the use of the context but after hours of reading and trying I open a new Thread. 因此,我知道关于AJAX和上下文的使用有很多线程,但是经过数小时的阅读和尝试后,我打开了一个新线程。

So I have this (shorten version) javascript function: 所以我有这个(缩短版本)的JavaScript功能:

this.CallService = function () {
    var Type = this.Type;
    var Url = this.Url;
    var Data = this.Data;
    var ContentType = this.ContentType;
    var DataType = this.DataType;
    var ProcessData = this.ProcessData;

    var ClipUrl = this.ClipUrl;
    var CountMax = this.CountMax;
    var Callback = this.Callback;

    var SucceededServiceCallback = this.SucceededServiceCallback;
    var FailedServiceCallback = this.FailedServiceCallback;


    return $.ajax({
        type: Type, //GET or POST or PUT or DELETE verb
        url: Url, // Location of the service
        data: Data, //Data sent to server
        contentType: ContentType, // content type sent to server
        dataType: DataType, //Expected data format from server
        processdata: ProcessData, //True or False
        context: this,
    }).done(function (msg) {//On Successfull service call
        SucceededServiceCallback(this, msg);
    }).fail(function (msg) {
        FailedServiceCallback(this, msg);
    });
}

The Important part here are the context: this and the two callbacks done and fail . 这里的重点是context: this和两个回调donefail Im those two callbacks I give the this context to my callback functions: 在这两个回调中,我this上下文赋予我的回调函数:

this.SucceededServiceCallback = function (context, result) {
    if (null != context) {
        UpdateDebugInfo(context, "succeeded: " + context.DataType + " URL: " + context.Url + " Data: " + context.Data + " Result: " +result);
    }
    if (context != null && context.DataType == "json" && result != null && context.Callback != null) {
        context.Callback(context, result);
    }
}

Here the important part is that I use the context to see access the variables DataType, Callback, Url etc. 这里重要的部分是我使用上下文查看访问变量DataType,Callback,Url等。
The Problem now is that the context is set to the last context used (it's an asynchron call so all the variable are the variable from the last call). 现在的问题是,上下文被设置为最后使用的上下文(这是一个异步调用,因此所有变量都是上次调用中的变量)。 So I'm pretty sure something is wrong with that context: this, part. 因此,我很确定该context: this,部分。 I just don't know how to use this right. 我只是不知道如何使用这项权利。 Thanks for your help. 谢谢你的帮助。

tl;dr: tl; dr:
I use context: this in an Ajax call. 我使用context: this在Ajax调用中。 Context is always set to the last "this" called. 上下文始终设置为最后一个“ this”调用。 I want to use the "this" of the call. 我想使用呼叫的“ this”。

You are "caching" all your variables before you fire each request, but in your SucceededServiceCallback function you are inspecting this.XXX - which is not the var Type it looks like you are expecting, but the actual this.Type itself. 在触发每个请求之前,您正在“缓存”所有变量,但是在SucceededServiceCallback函数中,您正在检查this.XXX这不是您期望的var Type ,而是实际的this.Type本身。

What you could do is put these properties into an object and pass it as context, rather than your main object: 您可以做的就是将这些属性放到一个对象中,并将其作为上下文(而不是主对象)传递:

this.CallService = function () {
    var context = {
        Type : this.Type,
        Url : this.Url,
        Data : this.Data,
        ContentType : this.ContentType,
        DataType : this.DataType,
        ProcessData : this.ProcessData,
        ClipUrl : this.ClipUrl,
        CountMax : this.CountMax,
        Callback : this.Callback
    };

    var SucceededServiceCallback = this.SucceededServiceCallback;
    var FailedServiceCallback = this.FailedServiceCallback;

    return $.ajax({
        type: Type, //GET or POST or PUT or DELETE verb
        url: Url, // Location of the service
        data: Data, //Data sent to server
        contentType: ContentType, // content type sent to server
        dataType: DataType, //Expected data format from server
        processdata: ProcessData, //True or False
        context: context,
    }).done(function (msg) {//On Successfull service call
        SucceededServiceCallback(this, msg);
    }).fail(function (msg) {
        FailedServiceCallback(this, msg);
    });
}

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

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