Search code examples
jqueryajaxjsonrestjsonp

Jsonp request return missing ; SyntaxError: missing ; before statement


i try this code

<body>
<script>

$.ajax({
    type: "GET",
    dataType: "jsonp",
    url: "http://www.viaggiatreno.it/viaggiatrenonew/resteasy/viaggiatreno/statistiche/1403508155490",
    success: function(result) {
        console.log("SUCCESS");
        console.log(result);
    },
    error: function(result) {
        console.log("ERROR");
        console.log(result);
    }
});                    

</script>

but the response is ** SyntaxError: missing ; before statement**

why?


Solution

  • The url that you are requesting returns the text:

    {"treniGiorno":2843,"ultimoAggiornamento":1403513918086,"treniCircolanti":524}
    

    The correct returned text should be:

    jsonCallback({"treniGiorno":2843,"ultimoAggiornamento":1403513918086,"treniCircolanti":524});
    

    Also consider adding the jsonpCallback parameter to your ajax query, like that:

    $.ajax({
        type: "GET",
        dataType: "jsonp",
        jsonpCallback: 'jsonCallback',
        url: "http://www.viaggiatreno.it/viaggiatrenonew/resteasy/viaggiatreno/statistiche/1403508155490",
        success: function(result) {
            console.log("SUCCESS");
            console.log(result);
        },
        error: function(result) {
            console.log("ERROR");
            console.log(result);
        }
    });