Ajax call returning whole page

2019-03-06 11:52发布

I have following page with some random tips: http://www.javaexperience.com/tips

I want to display the tips only on the other pages of website so I am making an ajax call and adding whatever returned by the ajax response to a Div's HTML.

The DIV html is:

<div id="tips"><div>

The ajax call is:

jQuery("#tips").load("/tips/");

The problem is that the ajax call results in whole page content to be added to div (since the page gets appended to the div, the above jQuery code gets invoked infinitely) where as I want to add only the tips section. Is there any easy way out?

2条回答
We Are One
2楼-- · 2019-03-06 12:23

This is the expected behavior of the load method, it'll always download the entire content. You can specify that a certain part of the loaded page be parsed and placed into the calling container.

jQuery('#tips').load('url #tip1');
查看更多
Explosion°爆炸
3楼-- · 2019-03-06 12:44

You need:

$('#tips').load('ajax/test.html #container');

More info on load here

查看更多
登录 后发表回答