You can add jsonp:'jsonp', to the ajax() call. The stackoverflow API documentation claims that it needs a jsonp query jsonp , and the jQuery property to configure the passed query string parameter is called jsonp . Without it, callback=? should be added by default at the end of the url.
I get success in the console by running this:
var URL = "http://api.stackoverflow.com/1.1/"; var _url = URL + 'users?filter=locrizak'; $.ajax({ dataType: 'jsonp', jsonp: 'jsonp', // <--- add this url: _url, success: function(val) { console.log('success'); }, error: function(val) { console.log('error'); console.log(arguments); } });
In addition, jsonp:false, should not set false ; it must be true , otherwise the query string parameter will not be added.
Update:. It works correctly with jQuery v1.6.2 and uses the correct parameters as described in my original answer above. The callback function must be outside the jQuery anonymous function.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>JSONP Test</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function(){ $.ajax({ dataType:'jsonp', jsonp: 'jsonp', jsonpCallback: 'onJsonp', url: 'http://api.stackoverflow.com/1.1/users?filter=locrizak', success: function(data) { console.log('success', data); }, error: function(data) { console.log('error', data); } }); }); function onJsonp(data) { console.log('callback', data); }; </script> </head><body></body></html>
Update 2: Based on the comments, here is another version, this time wrapped in an object. Please note: you cannot use jsonpCallback: 'api.onJsonp', , because it is defined only in the anonymous jQuery function. The easiest way to preserve encapsulation is to create a global function and simply transfer control back to the api counterpart.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>JSONP Test</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function(){ api = { get: function(url) { var URL = "http://api.stackoverflow.com/1.1/"; $.ajax({ dataType: 'jsonp', jsonp: 'jsonp', jsonpCallback: 'onJsonp', url: URL + url, success: function(data) { console.log('success', data); }, error: function(data) { console.log('error', data); } }); }, onJsonp: function(data) { console.log('callback', data); } } api.get('users?filter=locrizak'); }); function onJsonp(data) { api.onJsonp(data); } </script> </head><body></body></html>