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"})
Related
First time using websockets with Go and getting a weird error that doesn't break the program, and still continue as if it was not a problem. The client is a ReactJS single page application.
JS Client:
const socket = new WebSocket("ws://localhost:5000/ws");
setConnection(socket);
socket.onmessage = (e) => {
const message = JSON.parse(e.data)
console.log("message:", message)
switch (message.Command) {
case "loginResult":
if (message.Result) {
console.log("login worked");
}else{
console.log("login did not work");
}
break;
}
}
Snippet of Go it is getting JSON from:
result := ws.LoginResult{
BaseMessage: ws.BaseMessage{
Command: "loginResult",
},
Result: false,
}
b, err := json.Marshal(result)
if err != nil {
fmt.Println(err)
return
}
if err = conn.WriteMessage(msgType, b); err != nil {
return
}
And the output:
in here
WebsocketProvider.tsx:20 message: {Command: 'loginResult', Result: false}
WebsocketProvider.tsx:27 login did not work
VM3502:1 Uncaught SyntaxError: Unexpected token l in JSON at position 0
at JSON.parse (<anonymous>)
at WebSocket.socket.onmessage (WebsocketProvider.tsx:19)
socket.onmessage # WebsocketProvider.tsx:19
Anyone have any idea why this is the case?
Solution was found:
It was to do with the migration of code and that a left over line was making a call that was ignored, hence where it made no difference to the execution. Nothing to do with the JSON format being difference since this was automatically generate by Go libraries.
the function you have written for onmessage will be run every time a valid websocket message has been received.
According to the debug log you have posted the bit that worked was when you did receive valid JSON from the server and the function ran to completion as evidenced by this line:
WebsocketProvider.tsx:27 login did not work
Mark the line number.
After this you get:
VM3502:1 Uncaught SyntaxError: Unexpected token l in JSON at position 0
at JSON.parse (<anonymous>)
at WebSocket.socket.onmessage (WebsocketProvider.tsx:19)
socket.onmessage # WebsocketProvider.tsx:19
mark the line number :19
This is the line with your JSON.parse.
My guess would be that this is a new invalid json message and it did indeed panic here and the rest of the function did not run - the bit that ran was a previous message with valid json.
As for why it failed - put in a console.log before your JSON.parse as suggested by #emptyhua to see what exactly you are receiving.
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.
I'm using request on my nodejs server to call an external JSON rest service.
This is a simplified example of my code :
var request = require("request");
request("http://www.sitepoint.com", function(error, response, body) {
var myJson = eval('(' + body+ ')');
});
It works well 90% of the time, but sometime I get this error :
Uncaught Syntax Error: Unexpected Token ILLEGAL
This error never refers to the same char into the received JSON, So in my understanding, the stream sent back by the rest service is truncated and cannot be converted to JSON.
How can I ensure myself that the request is done completly and the data complete ?
I am currently struggling with a JSON string that I receive from my server application.
This is the JavaScript snippet that receives the JSON string:
var connection = new WebSocket('ws://'+location.hostname+':81/', ['arduino']);
connection.onmessage = function (e) {
console.log('Received from server: ', e.data);
var response = JSON.parse(e.data);
if(response.action=="networks") {
console.log('SSID: ', response.ssid);
}
};
I get this response in my browser's console according to my console.log call above:
Received from server: {"action":"networks","ssid":"UPC6288862","rssi":-69,"enc":8}
Ending in the following error:
Uncaught SyntaxError: Unexpected token in JSON at position 60
at JSON.parse (<anonymous>)
at WebSocket.connection.onmessage (WebSocket.js:19)
connection.onmessage#WebSocket.js:19
When I manually put the string, to JSON.parse() like this:
var data = JSON.parse('{"action":"networks","ssid":"UPC Wi-Free","rssi":-42,"enc":255}');
the parsing works and I can access the fields by response.action for example.
But why I get the error? Is e.data not a proper string or do I need to add some quotes or similar to e.data before parsing?
UPDATE:
Here's a screenshot of Chrome's network tab while receiving the JSON string via the WebSocket.js:
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.