Search code examples
javascriptjqueryjsonpyql

Uncaught SyntaxError: Unexpected token : YQL jQuery


I'm trying to get data from a JSON via Yahoo Query Language (YQL) using jQuery.

Link to JSON

index.html

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>

    $.ajax({
        url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'http%3A%2F%2Fwww.unisul.br%2Fwps%2Fportal%2Fhome%2Fconheca-a-unisul%2Fa-universidade%2Fcampus-unisul-virtual%2Fpolos-presenciais'%20and%20xpath%3D'%2F%2F*%5B%40id%3D%22lista-polos%22%5D'&format=json&diagnostics=true&callback=",
        dataType: 'jsonp',
        success: function (response) {

            var polos = response.results[0];
            var getPolosHTML = '';

            console.log(polos);

        }
    });
</script>

Error in console: Uncaught SyntaxError: Unexpected token :

Any solution?


Solution

  • First, you have to setup the url so that jquery will add in the jsonp param by adding a ? to callback=

    ...&callback=?
    

    then, you have to modify the success callback to properly reference the data.

    var polos = response.query.results;
    console.log(polos); // object with a div property
    

    http://jsfiddle.net/jZ4n8/1/