Adam Starrh Adam Starrh - 1 year ago 69
jQuery Question

Django/Ajax Get "data" returning html from firing page instead of target view

I'm using the following simple AJAX call.

url: $('#currentNodes').attr('target'),
type: 'get',
dataType: "html",
success: function(data) {
failure: function(data) {

It is supposed to grab the target from the following element:

<p><a href="{% url "current_nodes" view.slug %}" id="currentNodes">Test</a></p>

When I click on the above element, my browser displays the HTML I set up in my

def current_nodes(request, slug):
html = "<html><body>Smoke Test for {}</body></html>".format(slug)
return HttpResponse(html)

at the url defined in my

url(r'^(?P<slug>[a-zA-Z0-9-]+)/current_nodes/$', current_nodes, name="current_nodes"),

But according to
, the Ajax call from this link receives the response that comes from the view that is serving the current page, not the one that I ask for in the target. Sure enough, if I try to do anything with it, it displays the full html of the template I am on, rather than plugging in the html I'm trying to process.

What am I missing?

Answer Source

#currentNodes is an a element. It doesn't have a target attribute; it has an href.

url: $('#currentNodes').attr('href'),
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download