简体   繁体   English

尝试从AJAX解析JSON数组时出现意外的令牌u

[英]Unexpected token u when trying to parse a JSON array from AJAX

I have a PHP script which is used for a POST request which returns (echos) the array: 我有一个PHP脚本,用于返回(echos)数组的POST请求:

array(3) {
  ["message"]=>
  string(32) "The item was successfully saved"
  ["newItemId"]=>
  int(9)
  ["newCustomFieldIds"]=>
  string(3) "[9]"
}

My AJAX request: 我的AJAX请求:

$.ajax({
        url: 'updateItemDetails.php',
        method: 'post',
        data: {
            ...
        }
    }).done(function(response) {
            console.log(response); //correct
            console.log(response['newCustomFieldIds']); //undefined
            console.log(JSON.parse(response['newCustomFieldIds'])); //unexpected token u
    });

The first console.log produces: 第一个console.log产生:

{"message":"The item was successfully saved","newItemId":9,"newCustomFieldIds":"[9]"}

which is as expected. 正如预期的那样。 However, the second one gives undefined . 但是,第二个给出undefined

So, when I JSON.parse, I get Uncaught SyntaxError: Unexpected token u ! 因此,当我进行JSON.parse时,我得到了Uncaught SyntaxError: Unexpected token u What I'm trying to do is to get the "[9]" to a real array, ie. 我想做的是将"[9]"转换为真实数组,即。 [9] . [9]

I don't understand this. 我不明白 newCustomFieldIds definitely exists, because when logging the response , I can see it -- so why doesn't the console.log(response['newCustomFieldIds']) work? newCustomFieldIds确实存在,因为在记录response ,我可以看到它-为什么console.log(response['newCustomFieldIds'])不起作用?

You have to parse response : 您必须解析response

JSON.parse(response).newCustomFieldIds

If you want to convert the string "[9]" to an array, you need to call JSON.parse twice: 如果要将字符串"[9]"转换为数组,则需要调用JSON.parse两次:

JSON.parse(JSON.parse(response).newCustomFieldIds)

However, the better solution would be to encode the value of newCustomFieldIds as an array in the first place, ie the JSON should contain "newCustomFieldIds": [9] . 但是, 更好的解决方案是首先将newCustomFieldIds的值编码为数组,即JSON应包含"newCustomFieldIds": [9]


Since you know that response['newCustomFieldIds'] returns undefined , it doesn't make sense to try JSON.parse(response['newCustomFieldIds']) . 由于您知道response['newCustomFieldIds']返回undefined ,因此尝试JSON.parse(response['newCustomFieldIds'])没有意义。 It's the same as JSON.parse("undefined") , but undefined is not valid JSON. JSON.parse("undefined") ,但undefined无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JSON.parse时,&#39;未捕获的SyntaxError:意外的标记u&#39; - 'Uncaught SyntaxError: Unexpected token u' when using JSON.parse NodeJS Ajax没有传递数组,错误:“ JSON在位置0处出现意外令牌u” - NodeJS Ajax not passing array, error: “Unexpected token u in JSON at position 0” 意外的令牌:你的JSON.parse()问题 - Unexpected token: u JSON.parse() issue 未捕获到的SyntaxError:JSON中的意外令牌u在JSON.parse的位置0 - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse JSON.parse 位置 0 处的 JSON 中的意外标记 u - Unexpected token u in JSON at position 0 at JSON.parse 语法错误:在 JSON 中的意外令牌 u 在 position 0 在 JSON.parse - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse SyntaxError:JSON中的意外令牌u在JSON.parse( <anonymous> ) - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) JSON 解析错误:尝试解析数组中的对象时出现意外的标识符“未定义”(React-Native) - JSON Parse error: Unexpected identifier "undefined" when trying to parse an object from an array (React-Native) 在 REST 响应上使用 JSON.parse(...) 时出现 Unexpected Token U 错误 - getting Unexpected Token U error when using JSON.parse(…) on REST Response 解析错误JSON.parse(意外令牌u) - parsing error JSON.parse(unexpected token u)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM