I've tried many things and there's no way, always appears this error I tried to use only one option to see if passed, changed the call of jquery, but not.
I looked in various places on the internet about this error, but could not solve or understand why it is happening. On my pc using EasyPHP works perfectly, but when I put online does not work.
Syntax Error: unexpected token <
Here's my code:
$(function(){ $('#salvar').click(function(){ var key = 'salvar'; var title = $('#title').val(); var opcao1 = $('#opcao1').val(); var opcao2 = $('#opcao2').val(); var opcao3 = $('#opcao3').val(); var opcao4 = $('#opcao4').val(); var opcao5 = $('#opcao5').val(); var opcao6 = $('#opcao6').val(); if(title.length > 0){ if(opcao2.length > 0){ $('#resposta').removeClass().html('Salvando a enquete...<br clear="all"><br><img src="images/switch-loading.gif" />'); $.ajax({ type : 'POST', url : 'funcoes/enquete_adm.php', dataType : 'json', data: {key:key,title:title,opcao1:opcao1,opcao2:opcao2,opcao3:opcao3,opcao4:opcao4,opcao5:opcao5,opcao6:opcao6}, success : function(data){ if(data.sql == 'ok'){ $('#resposta').addClass('success-box').html('Enquete Salva!').fadeIn(1000); $('#control').fadeOut(); }else if(data.sql == 'error'){ $('#resposta').addClass('info-box').html('Ops, aconteceu um erro. Por favor, tente novamente').fadeIn(1000); } }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert("XMLHttpRequest " + XMLHttpRequest[0]);alert(" errorThrown: " + errorThrown);alert( " textstatus : " + textStatus); } }); }else{ $('#resposta').addClass('warning-box').html('É necessário no mínimo duas opções'); }; }else{ $('#resposta').addClass('warning-box').html('Coloque a pergunta da enquete'); }; return false; }); }); // End
As you write your JavaScript application, the unexpected token error always occurs because JavaScript expected a specific syntax that's not fulfilled by your current code. You can generally fix the error by removing or adding a specific JavaScript language symbol to your code.
To solve the "Uncaught SyntaxError: Unexpected identifier" error, make sure you don't have any misspelled keywords, e.g. Let or Function instead of let and function , and correct any typos related to a missing or an extra comma, colon, parenthesis, quote or bracket.
The "Unexpected token u in JSON at position 0" error occurs when we pass an undefined value to the JSON. parse or $. parseJSON methods. To solve the error, inspect the value you're trying to parse and make sure it's a valid JSON string before parsing it.
Unexpected TokenThis is simply down to a syntax error (your fault, I'm afraid). Perhaps you forgot the ':' after a conditional branch or there is an unclosed parenthesis left somewhere in your code? Python scripts are broken down into 'tokens' which helps the program navigate the logic of your code.
This usually happens when you're including or posting to a file which doesn't exist. The server will return a regular html-formatted "404 Not Found" enclosed with
'<html></html>'
tags. That first chevron < isn't valid js nor valid json, therefore it triggers an unexpected token.
What if you try to change 'funcoes/enquete_adm.php' to an absolute url, just to be sure?
EDIT (several years later)
The root cause might not always come from 404 errors. Sometimes you can make a request to an API and receive HTML formatted errors. I've stumbled to a couple of cases in which the API endpoint should have returned
{ error: "you must be authenticated to make this request" }
With header 401. And instead I got
<html>You must be authenticated to make this request</html>
With header 200.
Given the header is 200 you can't tell the request has failed beforehand, and you're stuck to try and JSON.parse
the response to check if it's valid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With