Adam Starrh Adam Starrh - 4 months ago 11
jQuery Question

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

I'm using the following simple AJAX call.

$.ajax({
url: $('#currentNodes').attr('target'),
type: 'get',
dataType: "html",
success: function(data) {
console.log(data);
},
failure: function(data) {
alert('error');
}
});


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 views.py:

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


at the url defined in my urls.py

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


But according to
console.log
, 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

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

url: $('#currentNodes').attr('href'),