How to pass Object context to jQuery.ajax JSONP callback? -
I run into problems to pass the JavaScript object context in the callback handler of a JSON AJAX request, when the ajax provider Its predefined call back. (Flickr service provider).
I give a simple example:
function person (AID) {this.name; This.id = anId; Var self = this; This.loadName = function () {$ .ajax ({url: "jsonp.json", datatype: "jsonp", jsonpCallback: "handleJSON", data: {id: this.id}, context: self, success: function (Data) {self.name = data.name;}}); }} Var Handson = function (data) {console.log ("Received" + Data.name); } Var a = new person (234); A.loadName (); Var B = new person (345); B.loadName ();
The above examples work perfectly, the console handle outputs to the line of the JSN function. But in this function I have no reference to the original object which is calling it. I want the success function to be called instead: Here I can refer to the self variable
There are many possible solutions, but I No work is going on.
- I stop the callback name which jQuery generates for me and puts it as a value in the JsonpCellback parameter I think this function is specified success Represents for the function, so that I can access it. But I think there is no way to get the generated name.
- I will specify the reference object as I did in the example. JQuery docs indicates that this object will be available for callback handlers. Although I do not know how to reveal this context, it has come out. handleJSON function from my example.
Currently I get a solution with jquery-jsonp from Google code. But still I'm very curious how to solve the problem as explained on 2. Because I think the context of jQuery says that it can be handled in this way. Does it make me less dependent on third party libraries
Can anyone show me how to use context in JSONP callback handler using jQuery?
simply jsonpCallback
leave the property alone, and give jQuery the function Let's create that and its function name is that the function will specify the referred callbacks to you.
function person (AID) {this.name; This.id = anId; This.loadName = function () {$ .ajax ({url: "jsonp.json", datatype: "jsonp", data: {id: this.id}, reference: this, success: work (data) {name = Data.name;}}); }} Var a = new person (234); A.loadName (); Var B = new person (345); B.loadName ();
Comments
Post a Comment