I’m having an issue parsing a jsonP request from Twitter api.
My code is as follows:
var processRequest = function(data){
console.log(data); //outputs what I believe to be correct
var result = $.parseJSON(data);
var text = '';
console.log("1");//outputs
$.each(result, function(){
console.log("2");// doesn't output
text = this['created_at'];
});
console.log(text);//doesn't output
}
$(document).ready(function () {
var url = "https://api.twitter.com/1/statuses/user_timeline.json?include_entities=0&include_rts=0&screen_name=DockersHamptons&count=1&callback=processRequest";
$.ajax({
type: "GET",
url:url,
dataType: 'jsonp',
data: {},
success: function (data){
if(typeof(data.error) != 'undefined') {
if(data.error != ''){
//Shouldn't hit here
}
} else {
//Shouldn't hit here.
}
}
});
});
The error I get in the firebug console is:
TypeError: a is null
But it says that error is located in my “jquery-1.7.1.min.js” file on line 2.
I also tried playing around with this in jsfiddle and this setup doesn’t writing anything to the <span></span>:
But this setup does:
Does this mean there is something in the twitter api that is causing it to come up as invalid json format?
EDIT:
I’m still having the same issue with this, but I wanted to add to it in case it might help.
Jquery is defined by the time it hits the line with the $.parseJSON() because I tried adding jquery code before and after that line. So the issue is definitely with what is returned by twitter’s api. But I find it hard to believe they are giving me invalid json formated data. So is there something I have to do to the result before using $.parseJSON()?
I had found the solution on another site. My issue was that twitter api already returns a well formed json string. So I just had to remove the use of $.parseJSON().