Reputation: 13
I'm new to javascript and web development in general.So this is an app to retrieve and display the last 10 tweets of a Twitter user.It uses AJAX to retrieve the tweets from the Django-powered server.The app works fine on my local machine but when i deploy it on pythonanywhere, the JavaScript part shows a Syntax error. Following is the script part in the HTML file.
<script>
$("#HandleForm").on("submit", function (event) {
event.preventDefault();
var handle = $('#handle').val();
get_tweets(handle);
});
function get_tweets(handle){
$.ajax({
url : "ajax/returntweets/",
type : "GET",
data : {handle_data : handle},
success : function(json){
var response = $.parseJSON(json)['tweet_data']
$('#tweet_table').append('<li class="list-group-item list-group-item-success">Tweets of @' + handle + '</li>');
for(i=0;i<response.length;i++){
$('#tweet_table').append('<li class="list-group-item">'+response[i]+'</li>');
}
$('#tweet_table').append('<li class="list-group-item list-group-item-warning">Tweets End Here!</li>');
},
});
}
</script>
This is the views.py returning the tweet data:
auth = OAuth1(APIKey, APISecret, AccessToken, AccessTokenSecret)
username = request.GET.get('handle_data')
verificationUrl = ' https://api.twitter.com/1.1/users/show.json?screen_name=%s' %username
response = requests.get(verificationUrl, auth=auth)
tweets = []
if response.status_code == 200:
requestUrl = ' https://api.twitter.com/1.1/statuses/user_timeline.json?tweet_mode=extended&screen_name=%s&count=10' %username
r = requests.get(requestUrl, auth=auth,)
for tweet in r.json():
tweets.append(tweet['full_text'])
else:
tweets.append("Invalid Handle")
response_data = {"tweet_data" : tweets}
return HttpResponse(json.dumps(response_data))
The browser console shows the following error
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
console.log(json)
prints the whole HTML document.
Upvotes: 1
Views: 157
Reputation: 11695
use JSON.parse
instead $.parseJSON
like below code
json_string = "{\"name\": \"google\"}"
json_data = JSON.parse(json_string);
Upvotes: 1