Uncaught SyntaxError: Unexpected token < in>) at Object.

前端 未结 5 947
终归单人心
终归单人心 2020-12-20 16:57

i have an error in JSON.parse(), i have .php file which contain method to retrieve data from database and .js file for autoComplete function, my .php file return data as str

相关标签:
5条回答
  • 2020-12-20 16:59

    It's server side error. You can check the value returned from server(php code) using browser's developer tools like firefox and check the response message.

    In addition, maybe you can remove JSON.parse() and add dataType: "json" inside the ajax function

    0 讨论(0)
  • 2020-12-20 17:08

    The error is within your server side, when there's an error on your server side, the response comes with html tags '<' when there's an error php will add tag with the error message. Therefore your json contains the html tags and becomes invalid because of unexpected tags.

    The error is within this array

    $eventstArray[] = array
            (
                'label'         => $eventsQuery2['eventTitle'];
                'venue'         => $eventsQuery2['venueName'];
                'category'      => $eventsQuery2['catDesc'];
                'price'         => $eventsQuery2['eventPrice'];
                'description'   => $eventsQuery2['eventDescription'];
            );
    

    it should be

    $eventstArray[] = array(
                'label' => $eventsQuery2['eventTitle'],
                'venue' => $eventsQuery2['venueName'],
                'category' => $eventsQuery2['catDesc'],
                'price' => $eventsQuery2['eventPrice'],
                'description' => $eventsQuery2['eventDescription']
            );
    

    (The problem source was the semi-colon(;) after the description value. It should be only at the end of array)

    0 讨论(0)
  • 2020-12-20 17:10

    Check this option if you are sure that your php code is correct but the returned json string cannot be parsed.

    I had a similar problem. What I found was that my PHP code had been saved as UTF8 but without the No-BOM option. That added three extra chars: ο»Ώ (hex: EF BB BF) at the beginning of the file causing the returned json string to have those extra chars at the beginning.

    here is how the beginning of my php file: ο»Ώ < ?php include...

    preview of code in hex mode

    0 讨论(0)
  • 2020-12-20 17:16

    Check if only the json encoded response is being thrown back only. If you do things like var dump in the php script make sure you comment it out as it also get attached to your javascript response

    0 讨论(0)
  • 2020-12-20 17:24

    That error is normally seen when the value given to JSON.parse is actually undefined. So, I would check the code that is trying to parse this - most likely you are not parsing the actual string shown here.

    0 讨论(0)
提交回复
热议问题