如何使用Jquery对一个类进行多个Ajax查询
我有以下代码。如果我硬编码的链接,它运行正确。如果我通知链接,我会一个接一个地通知所有不同的短url。但是,当我尝试将这些值传递到ajax调用时,代码会中断。请提供任何帮助:如何使用Jquery对一个类进行多个Ajax查询,jquery,ajax,Jquery,Ajax,我有以下代码。如果我硬编码的链接,它运行正确。如果我通知链接,我会一个接一个地通知所有不同的短url。但是,当我尝试将这些值传递到ajax调用时,代码会中断。请提供任何帮助: $(document).ready(function(){ //find all the shornened urls $.each($('.shortenedUrl'), function(index, value) { inline_stats_lookup(value);
$(document).ready(function(){
//find all the shornened urls
$.each($('.shortenedUrl'), function(index, value) {
inline_stats_lookup(value);
});
function inline_stats_lookup(theLink)
{
alert(theLink);
//var theLink = "http://goo.gl/b9N1k";
$.post('http://qrcodes.weddingdecorationss.com/tracking/inline_statistics', {url: theLink}, function(response, status, xhr) {
if (status == 'error')
{
var msg = "Sorry but there was an error: ";
$("#results").html(msg + xhr.status + " " + xhr.statusText);
}
else
{
//$('.clicksAllTime').empty().append('<p>' + response[0].analytics.allTime.shortUrlClicks + '</p>');
//$('.clicksToday').empty().append('<p>' + response[0].analytics.day.shortUrlClicks + '</p>');
}
}, "json");
}
});
$(文档).ready(函数(){
//查找所有被删减的URL
$.each($('.shortenedUrl'),函数(索引,值){
内联统计查找(值);
});
函数内联统计查找(theLink)
{
警报(链接);
//var theLink=”http://goo.gl/b9N1k";
$.post($)http://qrcodes.weddingdecorationss.com/tracking/inline_statistics“,{url:theLink},函数(响应,状态,xhr){
如果(状态=‘错误’)
{
var msg=“抱歉,出现错误:”;
$(“#结果”).html(msg+xhr.status+“”+xhr.statusText);
}
其他的
{
//$('.clicksAllTime').empty().append(''+响应[0].analytics.allTime.shortUrlClicks+'');
//$('.clicksToday').empty().append(''+response[0].analytics.day.shortUrlClicks+'');
}
}“json”);
}
});
问题在于,您正在遍历所有锚点,并传递值<代码>值
是一个HTMLanchoreElement
锚定的.toString()
方法返回锚定的href
值,这就是警报
打印URL的原因
您必须传递value.href
而不是value
,才能使代码正常工作:
$('.shortenedUrl').each(function() { // <-- index, value not used:
inline_stats_lookup(this.href); // this === value for elements
});
$('.shortenedUrl')。each(function(){//你确定你没有尾随空格或特殊字符未显示在警报中吗?Rob-非常感谢你的款待。从昨晚起,它一直让我抓狂!