如果我运行这个,我得到“发送”消息,但“收到”消息不显示。为什么?错误消息是失败和错误块中的无传输。为什么这个jquery ajax不起作用?
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>
编辑:接受的答案使用$ .getJSON它调用$就在内部。对于那些可能对低级$ .ajax感兴趣的人,在这里,你去。
$('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);
}
});
});
你有什么错误吗? – j08691
没有错误,没有任何东西。 –
你是否在任何devtools /萤火虫中看到你的请求? – lanzz