JSONP error: Uncaught SyntaxError: Unexpected token : - javascript

I am trying to get a json encoded object from here but I keep on receiving the following error "Uncaught Syntax Error: Unexpected token : ". I was wondering what is causing this error and what I could do to fix it. Thanks to anyone who can help. Here's my code.
<script>
function mycallback(answer){
var stuff = JSON.parse(answer);
alert(stuff);
console.log(stuff);
}
</script>
<script src="http://www.wcischeduleapp.com/app/get.php?callback=mycallback"></script>

The way JSONP works is that answer is already an object, not a JSON string any more.
So just console.log(answer); will work just fine.

Related

Uncaught SyntaxError: Invalid or unexpected token when using $(element).html to include jsp

I am brand new to JAVA (JSP and JSTL)
I make an Ajax call to an api endpoint and in the callback I want to load a jsp file, which contains jsp. So, I do something like
AjaxQueueManager.get("/xyz/abc/" + efg,
function(data) {
if (data.success) {
$(element).html('<jsp:include page="includes/jsp-file.jsp" />');
} else {
getResourcesFailureCallback(element, data.errors, "ocon-wrong", "Something's Wrong!");
}
},
function(error) {
console.log("#####", error.status, error.statusText);
}
);
If my jsp-file.jsp doesn't contain any javascript all works well for example, if I put
<p>Hello World</p>
it works fine, but as soon as I put javascript in the jsp file even the most basic javascript like
console.log("hello world");
I get an error
Uncaught SyntaxError: Invalid or unexpected token
$(element).html("<script type="text/javascript">
I understand the error, its cause of " getting mixed with the " of type="text/javascript" but I dont know how to fix it.

Uncaught SyntaxError: Unexpected token < using CakePhP

I'm meeting a small problem with CakePhp... When I get an url like http://www.example.com/news/delete:5 , I always get this javascript error message, no matter what param I want to transfer through the url.
jquery-2.1.4.min.js:1 Uncaught SyntaxError: Unexpected token <
jquery-ui.min.js:1 Uncaught SyntaxError: Unexpected token <
In those files, at the first line.
Anyone can help me please? Thank you!

Yummly API " Uncaught SyntaxError: Unexpected token : "

I am trying to request data from the Yummly API with the following call
$http.jsonp('http://api.yummly.com/v1/api/recipes?_app_id='
+ $scope.apiId
+ '&_app_key='
+ $scope.apiKey
+ '&allowedAllergy[]=396^Dairy-Free'
+ '/?callback=JSON_CALLBACK' ).success(function(data) {
console.log(data);
}).error(function(error) {
});
And I keep getting this error saying " Uncaught SyntaxError: Unexpected token : " and when I click it takes me to the response. It is also not logging the data in the console.
The sever is returning JSON - not JSONP. This in turns causes the exception when the JSON text is executed in the hosting <script> context.
Running the following code in the console will generate the same error because {..} is in a Statement context:
{"foo": "bar"}
On the other hand, a valid JSONP response should look like this (which is valid syntax because {..} is in an Expression context):
JSON_CALLBACK({"foo": "bar"})

Trying to parse JSON with underscore (Unexpected token on Chrome)

Hello I am trying to get a grasp on underscore.js
i have a json file as follows:
[
{
"name":"rocky",
"last-updated": "Yesterday",
"age":"32"
},
{
"name":"annie",
"last-updated": "Today",
"age":"31"
}
]
And a javascript function:
function getNames() {
var users = $.ajax({
url : "users.json",
async : false
});
var names = _.map(JSON.parse(users.responseText),
function(user) {
return user.name
});
return names;
}
It works fine on IE but on Chrome, it throws me:
Uncaught SyntaxError: Unexpected token ,
on this line:
var names = _.map(JSON.parse(users.responseText),function(user) {return user.name});
As far as I know this error is because of trying to parse object not the JSON string. Am i right? How do I solve this? It works on IE?
Thank you!
It turned out the problem was with url parameter.
url : "users.json"
url: "/users.json"
Error thrown by Chrome:
Uncaught SyntaxError: Unexpected token ,
After an hour of troubleshooting, i found out:
Chrome has a bug on caching GET requests.
It can be fixed by setting
cache: false
on my Ajax call!
Also making a directory and calling that directory on url seems to be working.
url : "json/users.json"
Thanks to those who tried to help.

Uncaught Exception error in Nodejs for get call

I am doing a JSON call using the request module in NodeJS but was getting
Error: Parse Error
at Socket.socketOnData (http.js:1367:20)
at TCP.onread (net.js:403:27)
and the process would exit without getting the response.
I put in a
process.on('uncaughtException', function(e){
console.log(JSON.stringify(e, null, ' '))
})
and got this as the trace ..
Parse Error
{
"bytesParsed": 238,
"code": "HPE_INVALID_CONSTANT"
}
I tried putting in a try catch block around the get call but that did not work.
Once I started catching the 'uncaughtException' I got the stack trace and then the JSON response for the call. Please help me identify why this is happening and how I can efficiently handle it. I am using node v0.8.12
HPE_INVALID_CONSTANT means that either the server sent 'HTTP/1.0' at the wrong time or that the Content-Length header was wrong. Either way, it's a problem with the server and its response that will need to be fixed on the server side.

Categories