2014-07-18 64 views
0

我想提出一个请求,从另一个页面加载内容,然后搜索该内容并提取我需要的信息。我一直试图使用jQuery.get()来做到这一点,但不能得到它的工作。使用jQuery.get()从另一个页面加载内容

这里是我使用的代码:

$.get('/category/page-2/', function(data) { 
    var theContent = $(data).find('#newWrapper img').attr('src'); 
    theContent.appendTo('#container'); // this isn't inserting the url string 

    console.log('the content: ' + theContent); // theContent returns undefined 
}, 'html'); 

我jQuery.load()工作,但我需要的多位信息进行返回的文档和不想做为每一个请求。这工作得很好:

$(".related-product").eq(0).load("/category/page-2/ #newWrapper img"); 
+4

是什么'data'的价值?你确定元素'#newWrapper img''存在'data'中吗?也''theContent'不是一个jQuery对象,你可以调用'theContent.appendTo()' –

+0

你可以做'console.log(data)'并在这里发布 –

+0

最后不要使用HTML,最好使用json –

回答

0

试试这个

jQuery.get('/category/page-2/', 
{ 
    ajax: true 
}, function (data) { 
    data = jQuery(data); 
    var theContent = jQuery('#newWrapper img', data).attr('src'); 
    theContent.appendTo('#container'); // this isn't inserting the url string 
    console.log('the content: ' + theContent); // theContent returns undefined 

}); 
+0

这会引发错误:'Uncaught TypeError:无法读取属性'appendTo'undefined' – roburidge

+0

你检查过了,是你的选择器'$('#newWrapper img')'在你的页面''/ category/page-2 /''上工作正常吗? –

+0

@ServerHalilov:是的,'$('#newWrapper img')'在''/ category/page-2 / – roburidge

相关问题