Javascript JSON无效字符语法错误-Dojo
向dojo 1.9.3请求/xhr回调函数发送JSONObject响应时,我收到一个无效字符synatx错误。我检查了JSON格式及其有效性。我被我做错的事弄糊涂了 正在发送的JSON格式:Javascript JSON无效字符语法错误-Dojo,javascript,json,servlets,dojo,Javascript,Json,Servlets,Dojo,向dojo 1.9.3请求/xhr回调函数发送JSONObject响应时,我收到一个无效字符synatx错误。我检查了JSON格式及其有效性。我被我做错的事弄糊涂了 正在发送的JSON格式: { "issuer":"CN=***** CA , OU=*******, O=*********, L=****, ST=***, C=**", "Thumbprint":"*********", "valid to":"Mon *************", "valid
{
"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>
据我所知,模块
dojo/request/xhr
不再使用form
属性。因此,我认为表单数据没有正确地发送到服务器
如果要向服务器发送表单数据,可以使用dojo/dom表单
模块,例如:
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方法发送,因为无法识别表单
属性,因此默认情况下,它将使用GET发送。我注意到您的表单使用POST,因此您应该使用方法属性来指定它
最好的方法是使用开发人员工具(通常通过在浏览器中按F12或Ctrl+Shift+I)检查您的网络请求。您的JSON是什么样子的?请帮助我。我知道的一切都试过了。必须使用Dojo,这对我来说是一个很大的挑战。@user3808671试图猜测。您正试图在out语句out.print(cert)中打印对象证书
尝试将JSON字符串cert.toString()输出为out.print(cert.toString())希望能帮上忙,我已经试过了。同样的结果。回调函数err给了我一个错误警报。新的结果是:一旦我运行它,就会提示我下载一个.json文件,该文件现在有json字符串响应。为什么它不显示在我的页面上呢?Dimitri,在目前的情况下,使用iframe相对于xhr有哪些优点和优点。我像你说的那样修改了代码,但是出现了更多的错误。开发者浏览器工具看起来不错,谢谢。我会继续使用它。使用iframe,我得到一个blank.html,其中包含加载响应数据失败和错误:“资源被解释为文档,但使用MIME类型application/json传输:”:“提示我下载一个.json文件。我正在使用chrome。通常,dojo/request/xhr
是最好的方式。XHR的唯一限制是不能使用跨域调用(除非提供CORS头)。这就是为什么有些变通方法是可用的,比如dojo/request/script
和dojo/request/iframe
,尽管它们都有缺点。dojo/request/iframe
模块的缺点是您无法读取响应主体,您得到的响应意味着发送某种HTML文档而不是JSON。这通常意味着返回错误页面。使用开发人员工具并查看网络选项卡以查看响应是什么。服务器日志也可以帮助您分析问题。Dimitri,我不确定上面的方法domFrom.toObject是否处理多部分/表单数据。获取一个错误错误500:javax.servlet.ServletException:SRVE8024E:请求不是多部分/表单数据类型。