Thursday, 12 September 2013

jquery ajax fails to parse valid json

jquery ajax fails to parse valid json

So I'm made a php script to output tweets in json and now I am trying to
parse with data with javascript. I've tested the data and it's valid json.
When I do the request in Example 2 it works fine. When I try to parse
using example 1 it fails saying Uncaught SyntaxError: Unexpected token N.
What is my problem?
Example 1
var request = $.ajax({
url: "http://website.com/twitter.php",
type: "GET",
data: {
twitter: 'google'
},
dataType: "json"
});
request.done(function(data) {
var resp = JSON.parse(data);
console.log(resp);
});
request.fail(function(jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
Example 2
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://website.com/twitter.php?twitter=google", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var resp = JSON.parse(xhr.responseText);
console.log(resp);
}
};
xhr.send();
JSON Data
["New Google Trends features\u2014including Trending Top Charts\u2014make
it easier to explore hot topics in #GoogleSearch g.co\/jmv6","Stating now,
join our Hangout w\/ President Barroso on the #SOTEU goo.gl\/FZCXaJ
#askbarroso","Explore the Galapagos Islands + see sea lions, blue-footed
boobies & other animals w\/ Street View in @googlemaps g.co\/ufjq","Slow
connections may keep Google Instant (results as you type) from working
smoothly. Our troubleshooting tip: g.co\/bve7","From @BBCNews: search VP
Ben Gomes on how search has become more intuitive & the next frontier
(includes video) goo.gl\/Z0ESkJ","\"Audio Ammunition\" - an exclusive
5-part documentary about the Clash from @GooglePlay g.co\/zrxn & on
youtube.com\/googleplay","justareflektor.com\u2013\u2013an interactive
@ChromeExp HTML5 film with @arcadefire, featuring their new single,
\u201cReflektor\u201d","#askbarroso about the State of the European Union
in a live conversation Thurs, Sept 12 g.co\/n3tj","Don\u2019t get locked
out: set up recovery options for your Google Account now g.co\/sm4k","It's
time for more transparency: our amended petition to the the U.S. Foreign
Surveillance Court g.co\/gkww"]

No comments:

Post a Comment