繁体   English   中英

jQuery.ajax在传递'%'(perc符号)时向JSON响应添加'(MISSING)'

[英]jQuery.ajax adding '(MISSING)' to JSON response when passing '%' (perc symbol)

我对此感到困惑:test.html:

<!DOCTYPE html>
<meta charset="utf-8">

<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>

<body>
  <script>
    console.log(jQuery.parseJSON('{"key" : "value_%"}'));  ## line 10
    $.ajax(
    { url : "/scripts/test.pl",
      success : function(resp) {console.log(resp); console.log(jQuery.parseJSON(resp))}
    }
    );
  </script>
</body>

</html>

/scripts/test.pl:

print '{"key":"value_%"}';

输出:

Object {key: "value_%"} test.html:10
{"key":"value_%"(MISSING)} test.html:13
Uncaught SyntaxError: Unexpected token ( jquery.min.js:3

即ajax响应正在更改JSON文本,并添加“(MISSING)”位,从而使parseJSON失败。

为什么会这样呢? 我应该如何避免呢?

M;

没有理由手动调用parseJSON。 只需使用dataType选项

$.ajax({ 
  url : "/scripts/test.pl",
  dataType: "json",
  success : function(resp) {console.log(resp);}
  } );

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM