Javascript error; Uncaught SyntaxError: missing ) after argument list - javascript

I keep getting this error (Javascript error; Uncaught SyntaxError: missing ) after argument list) when trying to call a simple function. Everything works without calling it in a function but I need to do it multiple times.
function myFunction(ip, port, div) {
$.get('http://mcping.net/api/'+ ip + ":" + port, function(data){
console.log(data.online);
$(div).html(data.online);
});
}
myFunction(162.223.8.210, 25567, #factionsOnline)

You're missing a parentheses because you didn't quote your strings
myFunction('162.223.8.210', '25567', '#factionsOnline');

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.

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.

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

Parse.com -- Cloud Code/JS -- "Cannot call method 'set' of undefined"

So, my Javascript isn't the best but I have to venture into it to run some Cloud Code functions. I have the following:
Parse.Cloud.define("setCommentIsTitle", function(request, response) {
Parse.Cloud.useMasterKey();
var query = new Parse.Query("Comment");
query.equalTo("objectId", request.params.objectId);
query.first({
success: function(object) {
object.set('isTitle', request.params.isTitle);
return object.save(); },
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
I logged the objectId I'm passing in as request.params.objectId and it's correct. What I don't get is that success is being called, but then I'm getting the following Cloud Code log when I console.log object:
I2013-10-21T17:27:52.120Z] object = undefined
And the following error returned in XCode:
code=141, error=TypeError: Cannot call method 'set' of undefined
If I'm calling the first function on query, and success is being called, shouldn't that mean there is an object returned? Why is object undefined?
OK, so this was a stupid error on my part, but also abetted by a confusing Parse error message.
My class is called Comments and not Comment, so I was looking up the wrong class. However, since success was called on the query I started looking in all the wrong places for the error.
Why would success be called if I'm querying a class that doesn't even exist??

Categories