JSON无效字符语法错误 - Dojo

时间:2014-07-06 01:24:40

标签: javascript json servlets dojo

在向我的dojo 1.9.3请求/ xhr回调函数发送JSONObject响应时,我收到无效字符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>

1 个答案:

答案 0 :(得分: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属性来指定。

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