jQuery $.ajax(), pass success data into separate function

前端 未结 6 818
难免孤独
难免孤独 2020-12-02 13:23

I am using the jQuery $.ajax() function. I have put this into a parent function, which passes some values into the ajax function. What I would like to do, is have a user d

6条回答
  •  情话喂你
    2020-12-02 14:15

    You can use this keyword to access custom data, passed to $.ajax() function:

        $.ajax({
            // ... // --> put ajax configuration parameters here
            yourCustomData: {param1: 'any value', time: '1h24'},  // put your custom key/value pair here
            success: successHandler
        });
    
        function successHandler(data, textStatus, jqXHR) {
            alert(this.yourCustomData.param1);  // shows "any value"
            console.log(this.yourCustomData.time);
        }
    

提交回复
热议问题