Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass context in jquery ajax success callback function

var Box = function(){     this.parm = {name:"rajakvk",year:2010};     Box.prototype.jspCall = function() {         $.ajax({             type: "post",             url: "some url",             success: this.exeSuccess,             error: this.exeError,             complete: this.exeComplete         });     }     this.exeSuccess = function(){         alert(this.parm.name);     } } 

I'm not getting Box object inside exeSuccess method. How to pass Box object inside exeSuccess method?

like image 647
rajakvk Avatar asked Oct 05 '10 12:10

rajakvk


People also ask

How do I return data after AJAX call success?

You can store your promise, you can pass it around, you can use it as an argument in function calls and you can return it from functions, but when you finally want to use your data that is returned by the AJAX call, you have to do it like this: promise. success(function (data) { alert(data); });

What is context in AJAX call?

context : An object to use as the context ( this ) of all Ajax-related callbacks. converters : An object containing dataType-to-dataType converters. crossDomain : Set this property to true to force a cross-domain request (such as JSONP) on the same domain.

How do you call a controller in a success with AJAX?

So, add a success option to your partial-view ajax request and then get the response and render it in the div you want the partial-view to be visible as: success: function(response){if(response!= null) $('<yourDivInWhich you want to render the partial view>'). html(response)} .

What is success in AJAX call?

success : A callback function that is executed if the request succeeds.it takes as an argument the returned data. It is also passed the text status of the response. dataType : The type of data expected from the server. The default is Intelligent Guess (xml, json, script, text, html).


1 Answers

Use the context option, like this:

    $.ajax({         context: this,         type: "post",         url: "some url",         success: this.exeSuccess,         error: this.exeError,         complete: this.exeComplete     }); 

The context option determines what context the callback is called with...so it determines what this refers to inside that function.

like image 151
Nick Craver Avatar answered Oct 05 '22 23:10

Nick Craver