You are here
JQuery AJAX Calls
Submitted by dstitely on Thu, 02/24/2011 - 10:55pm
I've been trying to make some JSON API calls using JWuery's getJSON/AJAX functions and am receiving 302 error codes. Has anyone had much lucking using java-script to access the API?
Thanks in advance,
Dan
Forums:
Hi Dan, JSON calls directly
Hi Dan,
JSON calls directly to the API technically should work. However, you are likely running into the Javascript security model known as "same origin". AJAX calls are not allowed to be made to servers other then the one that originated the Javascript making the call. In addition, you should be careful to not expose your TextWise API token in publicly served Javascript files. This could compromise your account.
Both these problems can be addressed by creating a limiting proxy with a server side application to keep your token safe. This server side application would call the TextWise API, and you would reference it from within Javascript.
A third method is to use JSONP. Which gets around the same origin policy. The TextWise API supports the "jsoncallback" parameter. This tells the API to wrap the JSON data in a function callback. This can be used in conjunction with the ajax function in JQuery. You would set the "dataType" option to "jsonp" and the "jsonp" option to "jsoncallback".
Example API Call With 'jsoncallback' Set:
http://api.s..h.com/TOKEN/category?uri=p&format=JSON&jsoncallback=myfunction
This would result in something like this:
myfunction({
"about": {
.....
"sourceUri": "p"
},
"categorizer": {"categorizerResponse": {"categories": [ {
"id": "26",
"weight": "0.9832386",
"label": "Arts/Design/Fashion"
}]}}
});