0

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
1
  • Functionally .get is the same as .getJson, getJson just prespecifies json. So no luck there. I get the same result. Oct 3 '15 at 3:21
0

Instead of fixing getJSON to accept my json, I snagged the url out and returned on it via php. Then I replaced my getJSON function with get and did not have to deal with json. Very frustrating but I was finally able to have a successful .done() instead of .fail(). I now have new problems, closing this question. Here is pertinent $.get().

 $.get("shorten.php?id="+place.postal_code)
        .done(function(data) {
            console.log("done");
            shorturl = data;
            console.log(shorturl);

            //shorturl = data;
        })
        .fail(function(errorThrown) {
            // log error to browser's console
            console.log("fail");
            console.log(errorThrown.toString());
        })        
        .always(function(){
            console.log("complete");
            //console.log(shorturl["id"]);
        });

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .