[Answer]-Passing RSS feeds to django view using AJAX

1👍

From the Ajax point of view

From what you write in the question it would be more appropriate to send a GET request rather than a POST request. You are not sending any information to these endpoints that they need to store, but only want to retrieve updates from them. This doesn’t change much of your code, you can do that with $.get() instead of $.post().


From the Django point of view

Sending a GET request is also easier, since you don’t need to account for ☞ Django’s CSRF protection. Issuing other ajax requests (like POST, DELETE, etc.) isn’t all that hard either, but it requires an extra step that involves sending the CSRF token with every request (well documented in the linked docs).


Rendering the result from the Ajax request

Currently there might be a conceptual problem with the template that you use to render the Ajax results, e.g. scorecenter/cricket.html. On the very top you are extending from index.html, which means that each request will not only return the block of html that you want to insert but also anything that’s part of index.html.

Also, since you will insert the results by Javascript into the already loaded website you don’t need to define template blocks. Let these templates only render exactly the html that you want to insert into the DOM with $('.testingData').html(data).


Debugging problems with Ajax requests

While developing your site, open the developer tools in your browser and activate the console there (either Firefox and Chrome are fine). Whenever an Ajax-request fails, it will display an error there, which you can then introspect.

Common errors are 404 (if the endpoint couldn’t be found), 400 or 403 if the request was not authorized or 500 if the server has an error processing your request. ☞ More complete list of status codes

Feel free to update your question with more details. Since this is a very wide topic be prepared that it might be closed by the community. In that case you can always search for more specific questions or if you don’t find answers, put them in a new question.

👤sthzg

Leave a comment