syntax error: unexpected token <

后端 未结 15 2029
一整个雨季
一整个雨季 2020-11-29 02:20

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

相关标签:
15条回答
  • 2020-11-29 03:22

    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.

    0 讨论(0)
  • 2020-11-29 03:22

    I was also having syntax error: unexpected token < while posting a form via ajax. Then I used curl to see what it returns:

    curl -X POST --data "firstName=a&lastName=a&email=array@f.com&pass=aaaa&mobile=12345678901&nID=123456789123456789&age=22&prof=xfd" http://handymama.co/CustomerRegistration.php
    

    I got something like this as a response:

    <br />
    <b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /home/handymama/public_html/CustomerRegistration.php:1) in <b>/home/handymama/public_html/CustomerRegistration.php</b> on line <b>3</b><br />
    <br />
    <b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /home/handymama/public_html/CustomerRegistration.php:1) in <b>/home/handymama/public_html/CustomerRegistration.php</b> on line <b>4</b><br />
    <br />
    <b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /home/handymama/public_html/CustomerRegistration.php:1) in <b>/home/handymama/public_html/CustomerRegistration.php</b> on line <b>7</b><br />
    <br />
    <b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /home/handymama/public_html/CustomerRegistration.php:1) in <b>/home/handymama/public_html/CustomerRegistration.php</b> on line <b>8</b><br />
    

    So all I had to do is just change the log level to only errors rather than warning.

    error_reporting(E_ERROR);
    
    0 讨论(0)
  • 2020-11-29 03:23

    Removing this line from my code solved my problem.

    header("Content-Type: application/json; charset=UTF-8"); 
    
    0 讨论(0)
提交回复
热议问题