Joe
Joe

Reputation: 8042

Generating a callback name for jsonp

I am making some jsonp requests. I know that jquery will make a good jsonp callback function name if you put callback=? on the URL. However, I am making the request in kind of a non-standard way.

So I was wondering if anyone has any good functions for generating a name for a function to wrap a jsonp response in?

Upvotes: 0

Views: 1500

Answers (1)

Brian B
Brian B

Reputation: 1551

You can generate a name yourself by simply keeping a global integer, increment it, and append it to a standard name, such as "jsonPCallback"

var jpSuffix=0;

function getNextCallbackName()
{
    return "jsonPCallback" + jpSuffix++;
}

But more likely, if you are making a specific JsonP call and always executing the same function, simply create that function with a single name, like "myJsonPCallback" that takes one parameter, and specify "?callback=myJsonPCallback" in your URL. The server should return code that then calls myJsonPCallback with the JSON data object as its one parameter.

Upvotes: 1

Related Questions