My google url shortener implementation as the "extra feature" has gone on for two weeks. I've struggled with this last little detail. Although I get a kick out of seeing most of the code function, the getJSON function is failing and kicking out an error (see below). Note - I've checked and double checked, written the js with different functions and the php entirely. And validated the json that is being returned. The jquery should behave like articles and update does, right?
javascript:
var placeparams = { geo : place.postal_code};
var shorturl = [];
$.getJSON("shorten.php", placeparams)
.done(function(data, textStatus, jqXHR) {
shorturl = $.parseJSON(data);
})
.fail(function(jqXHR, textStatus, errorThrown) {
// log error to browser's console
console.log(errorThrown.toString()); // fyi this is line 344
})
.always(function(){
console.log("complete"); // line 348
console.log(shorturl["id"]); // line 349
});
Relevant php
$geo = urlencode($_GET["geo"]);
$url = "http://news.google.com/news?geo={$geo}";
// lots of google api stuff here I have left out
//
//
$url = json_decode($shorturl);
header("Content-type: application/json");
print(json_encode($url, JSON_PRETTY_PRINT));
Valid JSON returned by Google:
{
"kind": "urlshortener#url",
"id": "http:\/\/goo.gl\/5dMKPE",
"longUrl": "http:\/\/news.google.com\/news?geo=02138"
}
Browser Console Output:
SyntaxError: Unexpected token < scripts.js:344
complete scripts.js:347
undefined scripts.js:348