How to use JSONP with object oriented Javascript

I am new to JSONP and have implemented cross-domain functions for my application and everything works fine. Now I want to change my javascript code to apply object orientation.

My api

http://localhost:8080/myApplication/getComments?callback=displayComments

CrossDomain.prototype.displayComments = function(data) {
    // code to display the comments
}

Now I get an error in firebug below

ReferenceError: displayComments is not defined

I changed api to

http://localhost:8080/myApplication/getComments?callback=this.displayComments

and found that the function was added to the line for a callback like this

http://localhost:8080/myApplication/getComments?callback=callback=function (jsonData)
{ 
    //code to display the comments
}

this time another bug in firebug

SyntaxError: function statement requires a name

I have doubts whether to use JSONP in object oriented javascript or not. Please, help.

Thanks in advance.

+3
source share
1 answer

It makes no sense to define a function prototype function if you are not going to instantiate this function, so start with this.

var myCrossDomain = new CrossDomain();

, ( , )

var uri = "http://localhost:8080/myApplication/getComments?callback=" + 
    encodeURIComponent("myCrossDomain.displayComments");

:

, js

, .

api

http://localhost:8080/myApplication/getComments?callback=this.displayComments

JSON-P. script. . this.displayComments, , window.displayComments.

, , .

, , , ( ) .

, ​​ ,

http://localhost:8080/myApplication/getComments?callback=callback=function (jsonData)

, URI, , .

+1

All Articles