if i run this, i get the “sending” messages but “received” message doesn’t show up. why? the error message is No Transport in the fail and error block.
html:
<ul>
<li><a href="http://www.hotmail.com">hotmail.com</a></li>
<li><a href="http://www.google.com">google.com</a></li>
</ul>
js:
<script type="text/javascript">
$(document).ready(function () {
$("a").each(function (index) {
var sUrl = 'http://tinyurl.com/api-create.php?url=' + $(this).attr('href')
alert('sending: ' + sUrl);
$.ajax({
url: sUrl,
crossDomain: true
}).fail(function (data) {
alert('failed: ' + data.statusText);
}).error(function (data) {
alert('error: ' + data.statusText);
}).done(function (data) {
alert('received: ' + sUrl);
});
});
});
</script>
edit: the accepted answer uses $.getJSON which calls $.ajax internally. for those who may be interested in low level $.ajax, here you go.
$('a').each(function (index) {
var app = 'http://json-tinyurl.appspot.com/';
var sUrl = app + '?url=' + $(this).attr('href') + '&callback=?';
$.ajax({
url: sUrl,
dataType: 'json',
success: function (data) {
alert('ajax:' + data.tinyurl);
},
error: function (jqXHR, textStatus, errorThrown) {
alert("jqXHR=" + jqXHR.responseText + "\ntextStatus=" + textStatus + "\nerrorThrown=" + errorThrown);
}
});
});
You are running into a problem because you are making a cross domain call and not using jsonp. See the answer here for a solution to your tinyurl generation problem:
Create TinyURL via Jquery Ajax call
Using that as reference, here is your fixed code: