Unexpected token < in JSON at position 2 jquery Autocomplete

吃可爱长大的小学妹 提交于 2019-12-05 09:14:58

My guess is that due to your malformed JSON data property, your server-side resource is returning an HTML error, hence the unexpected < character in the response.

Fix your data by creating a valid JSON string...

data: JSON.stringify({nome: request.term}),

This will produce a value like

{"nome":"whatever you typed"}

which is valid instead of

{'nome':'whatever you typed'}

which is not due to the single-quotes and possibly worse depending on the value of request.term.

RPichioli

Try to stringify the response data and parse it again, take a look:

(...)
success: function (data) {
    // ----------------------------------------------
    // My suggestion
    // ----------------------------------------------
    // Re-rendering the JSON -> Stringify > Parse
    data = jQuery.parseJSON(JSON.stringify(data));
    // Debugging the JSON object in console
    console.log(data); 
    // ----------------------------------------------

    debugger;
    callback($.map(data.cities, function (obj) {
        return obj.Main
    }))
}
(...)

You can see more about in a relative issue like: Parsing JSON giving "unexpected token o" error

valid json string must have double quote.

JSON.parse({"u1":1000,"u2":1100})       // will be ok

no quote cause error

JSON.parse({u1:1000,u2:1100})    
// error Uncaught SyntaxError: Unexpected token u in JSON at position 2

single quote cause error

JSON.parse({'u1':1000,'u2':1100})    
// error Uncaught SyntaxError: Unexpected token u in JSON at position 2

You must valid json string at https://jsonformatter.curiousconcept.com/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!