I am using jQuery for the first time for my new job. I'm currently working on building a Twitter integration where I call the Bit.ly API and shorten a URL. Here is my code (that literally was working a few days ago):
success: function (data)
if (data.status_code != 200)
alert ('There was an error, please retry construction of the tweet.');
shortened_url = data.data.url;
Using Firefox with Firebug, I get a 200 OK for the request, but it gives me the error of:
data is null.
Anyone have any idea WHY this wouldn't be working and how to fix this? I need a helpful nudge ASAP if possible.
cross-domain calls are not cross-browser. i can't explain why it would work at one point but not another other than maybe you tested in IE and it worked in IE. the solution is to do the work on the server. have your ajax call a script on your server that then calls the bit.ly api, and returns the result to your ajax.
does bit.ly api support jsonp? if it does, you can use jsonp, just not json. you would have to change your "dataType" to "jsonp"
Edit: yes it does support jsonp, just change your dataType to "jsonp" and it should work. although, u will have to make async: true because jsonp can only be asynchronous.
So, this may sound stupid (my AJAX skills are rather bad), but is there a way to call a script on my server to get the shortened URL and return Json results? How would I do such a thing (I'm sorry, I'm really bad at Ajax...)?
Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation.
if you want it to be async:false, you'll have to make a server proxy to retrieve the data.
basically, you send the same request that you would send to the api to a script on your server instead. then, you get your server to make the cross-domain json request, then pass the data back to your ajax script.
Edit: however, i still suggest you use asynchronous, i'm pretty sure the code can be re-worked to work that way. you would just place all processing that is to be ran after the ajax call inside of the ajax success method.
I honestly didn't even think of that! Kevin, you are a genius in my book. My code now needs some major rework (I have 3 different functions, all that do different stuff and do it in order... AJAX was what threw that off... Thanks so much! I will post back with the results soon.