user1524625
user1524625

Reputation: 281

How do I include django URL in AJAX GET request and get JSON data?

I am running a development server and calling a template view to show me content of "index.html" on my browser. Now I have a button configured in the index.htm file (using JQUERY) to do AJAX get request. How do I include the Django url to do this ?

$(document).ready(function(){
$("#btn1").click(function(){
//alert("Text: " + $("#test").text());
//alert("Text: " );


$.ajax({
url: 'application.json',
dataType: 'json',
success: function( data ) {
  alert( "SUCCESS:  " + data );
},
error: function( data ) {
  alert( "ERROR  " + data );
}
});

I want to replace the 'application.json' with a django url which would return me a json response.

here is the return values asscociated with the django view correspoing to the url I wan to include:

response = Response(200, result)
return self.render(response)

Upvotes: 0

Views: 854

Answers (1)

robertof
robertof

Reputation: 131

$.ajax({
  url: {% url 'name-of-your-reverted url' %}
...

You can also use view path, just as in doc: Django Doc#url

Upvotes: 1

Related Questions