Ajax Call and Clean JSON But Syntax Error: Missing ; Before Statement

AJAX call and clean JSON but Syntax Error: missing ; before statement

JSONP is not JSON. A JSONP response would consist of a JavaScript script containing only a function call (to a pre-defined function) with one argument (which is a JavaScript object literal conforming to JSON syntax).

The response you are getting is JSON, not JSONP so your efforts to handle it as JSONP fail.

Change dataType: 'jsonp' to dataType: 'json' (or remove the line entirely, the server issues the correct content-type so you don't need to override it).

Since your script is running on a different origin to the JSON then you will also need to take steps (most, but not all, of which require that you control the host serving the JSON) to work around the same origin policy.

SyntaxError: missing ; before statement jquery jsonp

If it's really JSON you ask for, don't set "jsonp" as dataType, and don't provide a callback :

$.ajax({
type: 'GET',
url: url,
contentType: "application/json",
success: function(json) {
alert(json);
},
error: function(e) {
console.log(e.message);
}
});

SyntaxError: missing ; before statement Json array

I am able to duplicate the error by omitting the variable assignment as shown below.

It would be helpful if you would explain this part of your question ...

when i try with a structure that is not in an array the code works but
with above i get a SyntaxError: missing ;

This example will return SyntaxError: missing ; before statement

<script type="text/javascript">
// var data =
{"status": "success",
"message": "",
"responseData": {
"leaderboard": [
{
"userid": 1,
"username": "username1",
"companyname": "Working com 1",
"currentlevel": 1
},
{
"userid": 2,
"username": "username2",
"companyname": "Working com 1",
"currentlevel": 13
}
]
}
}

</script>

This example works without an error:

<script type="text/javascript">

var data =

{"status": "success",
"message": "",
"responseData": {
"leaderboard": [
{
"userid": 1,
"username": "username1",
"companyname": "Working com 1",
"currentlevel": 1
},
{
"userid": 2,
"username": "username2",
"companyname": "Working com 1",
"currentlevel": 13
}
]
}
}

</script>

SyntaxError: missing ; before statement in JSON

You are requesting a jsonp response, but the response is plain JSON.

You can either change the request type to 'JSON' - if the request doesn't fall foul of cross-domain restrictions, or change whatever is generating the response to wrap the response as JSONP.

For more information on JSONP, have a look at the jQuery docs

In summary, jQuery automatically adds a callback=? parameter to the url it requests. Your server-side code needs to use the value of that parameter as the name of a javascript function your response should invoke with the JSON passed as an argument.

For example, if callback=helloWorld, your response should be:

helloWorld({"tweet": ... });

JSONP — SyntaxError: missing ; before statement or Cross-Origin Request Blocked

Create 1 additional php ie : testing.php file to get that JSON response , then call that php file from your ajax request.

PHP FILE

$url = 'https://api.gumroad.com/v2/products?callback=jQuery112409655243732650752_1495261525390&access_token=676234257caeb63ca7683c39d14e0091387a1a36af0c2135f989d0fd84ffc0c5&_=1495261525391';
header('Content-Type: application/json');
echo file_get_contents($url);

Javasript

jQuery.ajax({
url : 'testing.php',
method: 'GET',
success: function( data, txtStatus) {
console.log(data);
}
});


Related Topics



Leave a reply



Submit