问题
I am trying to do the following:
I need to download the headlines from BBC and CNN using Ajax and jquery in Django. I tried first to download the data in javascript but I am getting a 'Access-Control-Allow-Origin' so i worked out I needed to do this through the backend in django. this needs to be done synchronously so that the browser doesn't have to be refreshed to redownload the data.
The requirements for my program are:
- Build a web application that displays, side by side, the headlines from both BBC and CNN, using their RSS (XML) feeds.
- Your application should use jQuery’s support for Ajax.
- The Django backend should download the news from:
• http://feeds.bbci.co.uk/news/rss.xml
• http://rss.cnn.com/rss/cnn_topstories.rss
and serve those to the client upon the ajax request for a news update.
So far I have the following:
from django.shortcuts import render
import requests
def index(request):
context = {}
return render(request, 'home/Newshome.html', context)
def submit(request):
xml_news = requests.get('http://rss.cnn.com/rss/cnn_topstories.rss')
news = xml_news.content
return render(request, 'home/Newshome.html', {'news': news})
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
$(document).ready(function() {
$.ajax({
type: "GET",
url: "http://rss.cnn.com/rss/cnn_topstories.rss",
dataType: "xml",
success: function upon_success(xml) {
$(xml).find('item').each()
}
});
});
</script>
</head>
<body>
<h1>Top News: BBC versus CNN</h1>
{% for item in news %}
<li>{{ item }}</li>
{% endfor %}
</body>
</html>
Any help would be appreciated! or pointers to links that will help me
来源:https://stackoverflow.com/questions/42326777/retrieving-rss-xml-news-data-using-jquery-ajax-in-django