Javascript JSON返回URL两次
我在一个页面上有一个html表,其中的RAW有“url”,我试图从一个随机行中一次获取一个url,但是我的代码返回url,正如您所看到的,它返回url两次,一次没有json,另一次有json数据,因此我得到404 我只需要.json URL,而不是第一部分 如何去掉第一个不是json的url 这是密码Javascript JSON返回URL两次,javascript,jquery,json,Javascript,Jquery,Json,我在一个页面上有一个html表,其中的RAW有“url”,我试图从一个随机行中一次获取一个url,但是我的代码返回url,正如您所看到的,它返回url两次,一次没有json,另一次有json数据,因此我得到404 我只需要.json URL,而不是第一部分 如何去掉第一个不是json的url 这是密码 $(document).ready(function() { $(document).on('click', '#closepopup', function() { $("#popup
$(document).ready(function() {
$(document).on('click', '#closepopup', function() {
$("#popup").removeClass('popupslidein')
});
var tablelink = "https://test.com/pages/product-listing-for-popups.json"; //products url for json data
$.getJSON(tablelink, function(data) {
var table = data.page.body_html;
$('#popuptable').append(table);
startthepopups()
});
var suburbink = "https://test.com/pages/product-listing-suburbs-for-popups"; //suburb names in table rows
$.getJSON(suburbink, function(data) {
var suburb = data.page.body_html;
$('#popupsuburb').append(suburb)
});
var namelink = "https://test.com/pages/product-listing-names-for-popups"; //names in table rows
$.getJSON(namelink, function(data) {
var name = data.page.body_html;
$('#popupname').append(name)
});
function startthepopups() {
var popupstay = 10000;
var popuptrigger = 100000;
function triggerpopup() {
var getrandomtd = Math.floor((Math.random() * $('#popuptable tr').length) + 1);
var link = $('#popuptable tr:nth-child(' + getrandomtd + ')').text();
console.log(link);
var productname = '';
var getrandomsuburbtd = Math.floor((Math.random() * $('#popupsuburb tr').length) + 1);
var suburblink = $('#popupsuburb tr:nth-child(' + getrandomsuburbtd + ')').text();
var getrandomnametd = Math.floor((Math.random() * $('#popupname tr').length) + 1);
var randomname = $('#popupname tr:nth-child(' + getrandomnametd + ')').text();
$.getJSON(link + '.json', function(data) {
productname = data.product.title;
imagelink = data.product.images[0].src;
if (!$("#popup").hasClass("popupslidein")) {
$('#popupsomeone span.name').empty().append(randomname);
$('#popupsomeone span.location').empty().append(suburblink);
$('#popupimage').css('background-image', 'url(' + imagelink.split('.jpg')[0] + '_small.jpg)');
$('#popupproduct a').attr('href', link).empty().append(productname);
$("#popupagotext").empty().append(Math.round(Math.random() * 15 + 10));
$("#popup").addClass('popupslidein');
setTimeout(function() {
$("#popup").removeClass('popupslidein')
}, popupstay);
}
});
}(function loop() {
var random = Math.round(Math.random() * 10) * 100000 + popuptrigger;
setTimeout(function() {
triggerpopup();
loop()
}, 60000)
}());
}
});
$.getJSON()
倾向于将当前url附加到您传递的路径,如果它认为它是相对的。为了实现这一点,您可以尝试像这样使用$.getJSON()
。请记住,使用的协议将是此代码所在的当前页面
$.getJSON('//test.com/pages/product-listing-for-popups.json')
我还注意到,在您的代码中没有任何地方有http://www.test.com/products/product-name.json
,您确定要共享正确的代码片段吗
工作演示
以下两种将$.getJSON()
与完全限定的url一起使用的方法非常有效:
$(文档).ready(函数(){
变量url=”https://jsonplaceholder.typicode.com/todos/1";
//例1
$.getJSON(url)
.完成(功能(数据){
控制台日志(数据);
});
//例2
$.getJSON(url、函数(数据){
console.log(数据)
});
});代码>
$。getJSON()
倾向于将当前url附加到您传递的路径,如果它认为它是相对的。为了实现这一点,您可以尝试像这样使用$.getJSON()
。请记住,使用的协议将是此代码所在的当前页面
$.getJSON('//test.com/pages/product-listing-for-popups.json')
我还注意到,在您的代码中没有任何地方有http://www.test.com/products/product-name.json
,您确定要共享正确的代码片段吗
工作演示
以下两种将$.getJSON()
与完全限定的url一起使用的方法非常有效:
$(文档).ready(函数(){
变量url=”https://jsonplaceholder.typicode.com/todos/1";
//例1
$.getJSON(url)
.完成(功能(数据){
控制台日志(数据);
});
//例2
$.getJSON(url、函数(数据){
console.log(数据)
});
});代码>
获取整行文本与定位单元格文本相比是不寻常的。使用第n个子项,根据表的结构,集合中还可能包含多行。提供一个复制链接的链接获取整行文本与目标单元格文本不同。使用第n个子项,根据表的结构,集合中还可能包含多行。提供一个复制要添加到此答案的链接的链接-/
前缀url将匹配页面的当前协议。(httphttp
或https
)。您好@AnonymousSB,谢谢您的回答,但它没有解决问题。关于->“我还注意到,您的代码中没有url”,正如您在代码中看到的,“link”变量使用@shutupchigo的表行中的“url”值构建此url。您能提供data.page.body\u html
的结果吗?您的console.log(link)
行的输出是什么?要添加到此答案,url的/
前缀将匹配页面的当前协议。(httphttp
或https
)。您好@AnonymousSB,谢谢您的回答,但它没有解决问题。关于->“我还注意到,您的代码中没有url”,正如您在代码中看到的,“link”变量使用@shutupchigo的表行中的“url”值构建此url。您能提供data.page.body\u html
的结果吗?您的console.log(link)
行的输出是什么?