JSON.parse error: invalid character at position 1 [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
For some reason I'm getting a parse error when trying to send a post request to the server
$.post("../php/user_handler.php", formData, function(data) {
var result = JSON.parse(data);
if(result.status === 'error') {
$('#' + result.control + '-error').html(result.message).css('display', 'inline-block');
} else {
$('#form-message').html(result.message).css('display', 'inline-block');
}
});
The message I'm getting implies that the error is with the syntax of the first character of the first line, but I really don't understand how; I've checked it against several examples of how to make this request and it looks just fine to me.

Simply means that your string is not JSON. First character has to be "{" or "[" but it is not. Might be empty or start with something else. Dump the thing aout on the console.log before you try to parse it.Might help ;)

Related

Type Error: Cannot read property 'includes' of undefined [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
This works
console.log(comments[i].includes("NEW ENTRY"));
This doesn't work
while ((comments[i].includes("NEW ENTRY")) == false) {
//Some code
}
With the little information you give us, I would say that you do not manage the incrementation of your variable i.
const arrStr = ["hello", "asdf", "dfa"]
arrStr[0].includes("hel") // -> true
arrStr[1].includes("hel") // -> false
arrStr[2].includes("hel") // -> false
arrStr[3].includes("hel") // -> cannot read property 'includes' of undefined
If you look at the DCR's answers you will see how to manage the incrementation
both work just fine! It's likely that your i counter is wrong.
var comments=['try a little harder','NEW ENTRY',"another test"];
console.log(comments[1].includes("NEW ENTRY"));
var i = 0;
while ((comments[i].includes("NEW ENTRY")) == false) {
console.log(comments[i]);
i++;}

Getting undefined values from JavaScript array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am getting response something like this in the form JavaScript Object
{
"result": [
{
"invoice_prefix": "INV",
"maximum_invoice_no": "0009"
}
]
}
I am trying to fetch the value of invoice_prefix and maximum_invoice_no
I have written result[0].maximum_invoice_no but I am getting undefined .
I have tried a lot of other ways also but still getting undefined . Please help me and give me some hint
I tried that and it is working as expected. I hope this will be helpful.
let test = {
"result": [
{
"invoice_prefix": "INV",
"maximum_invoice_no": "0009"
}
]
}
document.getElementById("output").innerHTML = "Maximum Invoice No. : " + test.result[0].maximum_invoice_no;
console.log(test.result[0].maximum_invoice_no);
<p id="output"></p>

Javascript appears to be saying true == false [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have the following js function.
function showAttentionItem(sender)
{
debugger;
var assistanceRequired = sender.parent().hasClass("assistanceRequired");
if (assistanceRequired)
{
sender.children('.assistanceRequiredText').fadeToggle(0);
if (sender.children('.assistanceRequiredText').is(":visible"))
{
sender.children('.studentPerformanceText').hide();
}
}
if (!assistanceRequired)
{
if (sender.parent().hasClass("studentOutsideTargetRange"))
{
sender.children('.studentPerformanceText').fadeToggle(0);
}
}
}
What happens when I run it is, I hit the debugger line, and step through the code. assistanceRequired is true.
After executing sender.children('.studentPerformanceText').hide();, the next line it jumps to is sender.children('.studentPerformanceText').fadeToggle(0);, inside the if (!assistanceRequired) statement!
How could this possibly be happening?
This is guaranteed to work:
if (sender.parent().hasClass("assistanceRequired"))
{
// do whatever here
}
else
{
// do whatever ELSE here
}
Only one or the other can ever run when coded correctly since there is only one test and it will either be true or false the single time it is tested. There is no possibility for it to be changed by some side effect you are not aware of, which is most likely what is happening. This is not debatable.
If this does not work the way you want then something else is wrong that you are not showing and without an executable jsFiddle good luck convincing anyone that a fundamental thing like the if ... else construct is broken.
I can think of only one scenario to explain what you are seeing.
There's a double trigggering of showAttentionItem(), which, in the debugger, is perceived as a single call. On the first call, assistanceRequired is true and on the second it's false.

Unexpected token ! within if statement [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
this is my markup
http://i.imgur.com/j2rhUH3.png
I tried $('.post-form').find('p:first').text().indexOf("Logged") !=''), and try to detect whether Logged is set or not, but I got this error Unexpected token !
You get error Unexpected token ! because your statement
$('.post-form').find('p:first').text().indexOf("Logged") !='')
is not complete.
Use if condition and indexOf as shown below :-
if( $('.post-form').find('p:first').text().indexOf("Logged") > -1)
The indexOf() method returns the position of the first occurrence of a specified value in a string.
This method returns -1 if the value to search never occurs
indexOf returns integer not string.
It returns the position where the specified searchvalue occurs for the first time, or -1 if it never occurs.
You get error Unexpected token ! because your statement
$('.post-form').find('p:first').text().indexOf("Logged") !='')
is incomplete.
Following should work:
if($('.post-form').find('p:first').text().indexOf("Logged") >= 0){
...
}
See DEMO here.

I cannot find this syntax error [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm going to feel like a complete idiot once this is pointed out to me, but I've got a syntax error I cannot figure out where the issue is coming from. Here is my code (error appears on last line but I doubt its that line that caused that):
// handle GCM notifications for Android
function onNotificationGCM(e) {
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
PushWoosh.appCode = "33F93-5013B";
PushWoosh.register(e.regid, function(data) {
alert("PushWoosh register success: " + JSON.stringify(data));
}, function(errorregistration) {
alert("Couldn't register with PushWoosh" + errorregistration);
});
}
break;
Thanks guys, I'm feeling like an idiot here and had a frustrating day.
Your onNotificationGCM() function is not closed, and neither is the switch block contained within it. The JavaScript parser is expecting to see two additional close braces (}) but the input file terminates before they are seen.
My guess is that you need to add these two braces after your break; statement, prior to the assignment of PushNotification.prototype.register.

Categories

Resources