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
Injecting Js Functions into the Page from a Greasemonkey Script on Chrome
Get Decimal Portion of a Number with JavaScript
Query Firestore Database for Document Id
How to Remove Emoji Code Using JavaScript
How to Use Node.Js Module System on the Clientside
Javascript: Call a Function After Specific Time Period
What Are Closures and Callbacks
Convert String Array Representation Back to an Array
How to Remove a Class from Elements in Pure JavaScript
Converting 24 Hour Time to 12 Hour Time W/ Am & Pm Using JavaScript
How to Create a Session Using JavaScript
Capture the Close Event of Popup Window in JavaScript
How to Delete All Cookies with JavaScript