Uncaught SyntaxError: Unexpected token < using CakePhP - javascript

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!

Related

"Uncaught SyntaxError: Unexpected token l in JSON at position 0", but still works, why?

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.

MongoDB findAndModify SyntaxError: missing ) after argument list

db.RO_DECLINE_DATA.findAndModify("query":{"$or":[{"emailMessageID":345544985},{"textMessageID":345544985}]},"update":{"$set":{"emailMessageStatus":"FAILED","emailSentDate":null}});
db.INBOX_COLLECTION.aggregate("pipeline":[{"$match":{"dealerID":1224,"dealerDepartmentID":3994,"dealerAssociateID":{"$in":[68638,68891]}}},{"$group":{"_id":"$customerID"}}]);
I can't seem to figure out what is wrong with these queries?
Error
2021-07-14T01:28:06.106+0530 E QUERY [js] uncaught exception: SyntaxError: missing ) after argument list :
i don't think any ')' is missing

Json call on unauthorized url returns Unexpected token < in JSON at position

Making ajax call:
let json = this._super(url, type, options);
On return it will return login page(HTML code) since url is unauthorized the json returned is:
SyntaxError: Unexpected token < in JSON at position 2
at JSON.parse ()
at jQuery.parseJSON (jquery.js:8520)
at ajaxConvert (jquery.js:8846)
at done (jquery.js:9264)
at XMLHttpRequest.callback (jquery.js:9718)
Errors occured: ember-metal.js:3992 TypeError: Cannot read property ‘get’ of undefined
at adapter.js:28
at tryCatcher (rsvp.js:215)
at invokeCallback (rsvp.js:393)
at publish (rsvp.js:379)
at publishRejection (rsvp.js:314)
at rsvp.js:14
ember-metal.js:3992 SyntaxError: Unexpected token < in JSON at position 2
at JSON.parse ()
at jQuery.parseJSON (jquery.js:8520)
at ajaxConvert (jquery.js:8846)
at done (jquery.js:9264)
at XMLHttpRequest.callback (jquery.js:9718)
What may be the reason?
If the resource requires authentication, then you're likely getting redirected to a login page in which case you're trying to parse the HTML code of the page as JSON.
Check the Content-Type header of the response to verify the format of the data before trying to parse it, JSON typically uses the MIME type application/json, whereas HTML uses text/html.
If server requires authentication to access the resource, then you'll typically need to set an Authorization header in your request with appropriate credentials.

JSONP error: Uncaught SyntaxError: Unexpected token :

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.

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"})

Categories