【问题标题】:JSON invalid character syntax error - DojoJSON 无效字符语法错误 - Dojo
【发布时间】:2014-07-06 01:24:39
【问题描述】:

在向我的 dojo 1.9.3 请求/xhr 回调函数发送 JSONObject 响应时,我收到了 Invalid character synatx 错误。我用 JSONLint 检查了我的 JSON 格式,它是有效的。真的很困惑我做错了什么。

正在发送的 JSON 格式:

{
    "issuer":"CN=***** CA , OU=*******, O=*********, L=****, ST=***, C=**",
    "Thumbprint":"*********",
    "valid to":"Mon *************",
    "valid from":"*****",
    "version":2
}

Servlet 代码:

JSONObject cert = new JSONObject();
cert.put("version", x509certificate.getVersion());
cert.put("valid from", x509certificate.getNotBefore());
cert.put("valid to", x509certificate.getNotAfter());
cert.put("issuer", x509certificate.getIssuerDN().getName());
cert.put("Thumbprint", getThumbPrint(x509certificate));

System.out.println(cert);
System.out.println(cert.toString());
out.print(cert);

DOJO/HTML 代码:

<body class="claro">
<script>dojoConfig = {parseOnLoad: true}</script>
<script src='dojo-release-1.9.3/dojo/dojo.js' ></script>

<script type="text/javascript">
require(["dojo/dom", "dojo/on", "dojo/request/iframe", "dom/dom-form", "dojo/dom-  
 construct", "dojo/json", 
 "dojo/domReady!"],
function(dom, on, iframe, domForm, domConst, JSON){    
on(dom.byId("startButton"), "click", function(){    
domConst.place("<p>Requesting...</p>", "output");    
 iframe("addUser",{
  method:"POST",
  form:"theForm",
  handleAs: "json",
}).then(function(cert){    
alert("data received!");    
domConst.place("<p>data: <code>" + JSON.stringify(cert) + "</code></p>", "output");

 }, function(err){    
 alert("data denied!!!");    
 alert(err);
  }); }); });
</script>
<form id="theForm" method="post" enctype="multipart/form-data">
      <input type="text" name="fname" value="Hello" /><br />
  <input type="text" name="lname" value="World" /><br />
  <input type="file" name="fileName" value="World" /><br />
  <button type="button" id="startButton">Start</button>
 </form>
       <h1>Output:</h1>
      <div id="output"></div>
      <button type="button" id="startButton">Start</button>
       </body>
    </html>

【问题讨论】:

  • 你的 JSON 是什么样的?
  • 请帮助我。我已经尝试了我所知道的一切。必须使用 Dojo,这对我来说是一个很大的挑战。
  • @user3808671 试图猜测。您正在尝试在 out 语句 out.print(cert); 中打印对象 cert 尝试将 JSON 字符串 cert.toString() 输出为 out.print(cert.toString()); 希望它有所帮助跨度>
  • 嗨弗兰克,我已经试过了。结果相同。回调函数 err 给我一个错误警报。
  • 新结果:一旦我运行它,就会提示我下载一个 .json 文件,该文件现在具有 JSON 字符串响应。为什么它没有显示在我的页面上?

标签: javascript json servlets dojo


【解决方案1】:

据我所知,模块dojo/request/xhr 不再使用form 属性。所以,我认为表单数据没有正确发送到服务器。

如果您想将表单数据发送到服务器,您可以使用dojo/dom-form 模块,例如:

xhr("addUser",{
    data: domForm.toObject("theForm"),
    method: "POST",
    handleAs: "json",
}).then(function(cert){    
    alert("data received!");    
    domConst.place("<p>data: <code>" + JSON.stringify(cert) + "</code></p>", "output");
}, function(err){    
    alert("data denied!!!");    
    alert(err);

    // Handle the error condition
});

还要确保您使用正确的 HTTP 方法发送它,因为无法识别 form 属性,默认情况下它将使用 GET 发送它。我注意到您的表单使用 POST,因此您应该使用 method 属性来指定它。

最好的办法是使用开发人员工具检查您的网络请求(通常按 F12Ctrl+Shift+I 在您的浏览器中)。

【讨论】:

  • Dimitri,在当前情况下,使用 iframe 与 xhr 相比有哪些优势。我按照您所说的更改了代码,但出现了更多错误。开发者浏览器工具看起来不错,谢谢。我会继续使用它。使用 iframe,我得到一个空白.html,无法加载响应数据和错误:“资源解释为文档,但使用 MIME 类型 application/json 传输:“localhost:”我收到提示下载 .json文件。我正在使用 chrome。
  • 通常dojo/request/xhr 是最好的选择。 XHR 的唯一限制是您不能使用跨域调用(除非提供 CORS 标头)。这就是为什么某些变通方法可用的原因,例如 dojo/request/scriptdojo/request/iframe,尽管它们都有其缺点。
  • dojo/request/iframe 模块的缺点是您无法读取所需的响应正文。
  • 您得到的响应意味着发送了某种 HTML 文档而不是 JSON。这通常意味着返回错误页面。使用开发人员工具并查看网络选项卡以查看响应是什么。服务器日志也可以帮助您分析问题。
  • Dimitri,我不确定上述方法 domFrom.toObject 是否处理多部分/表单数据。收到错误。 – 错误 500:javax.servlet.ServletException:SRVE8024E:请求不是 multipart/form-data 类型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-14
  • 2021-08-21
  • 1970-01-01
  • 1970-01-01
  • 2012-09-20
  • 2021-04-21
  • 1970-01-01
相关资源
最近更新 更多