通过JSON的多部分请求没有得到响应?

时间:2013-04-23 08:33:41

标签: java javascript json

我在java中遇到的问题是,当我通过JSON发出请求时,多部分时间我无法从服务器获得响应。

这是javascript的reuest块:

$.ajaxFileUpload({ url: "/iview",
                  secureuri:false,
                 fileElementsId:fileIds,
                 dataType:entity.config.dataType?entity.config.dataType:"text",
                 data:{appmode:entity.config.appmode,   
                 json:requestObject,"__RequestType":"ajax"},
                 success: function (data, status){
                  for(var ent in fileIds){
                                    entityObject[ent].object = document.getElementById(fileIds[ent]);
                                    entityObject[ent].object.id = fileIds[ent];
                                }
                                requestResponse(data);
                            },
                            error: function (data, status, e){
                                Cyberoam.removeOverLay();
                                Cyberoam.messageBox({message:reportlabels.ConnectionLostMsg,autoClose:true});
                            },timeout:30*60*1000
                        }
                    );

下面是发送响应的servlet代码:

PrintWriter out =response.getWriter();
response.setContentType("text/plain");                
out.println(new String(("status").toString().getBytes("UTF-8")));
out.close();    

所以请帮忙解决这个问题,以获得JSON的多部分请求的响应?

提前感谢。

1 个答案:

答案 0 :(得分:2)

你明白这是做什么的吗?

out.println(new String(("status").toString().getBytes("UTF-8")));

让我们重写它,以便我们可以看到不同的部分。

String s1 = "status";
String s2 = s1.toString();
byte[] bytes = s2.getBytes("UTF-8");
String s2 = new String(bytes);
out.println(s2);

这就是发生的事情:

  • 您有一个包含“status”的字符串,并在其上调用toString()。这没什么。
  • 然后将字符串转换为字节数组。字节数组将包含以UTF-8编码的字符串内容。
  • 然后将字节解释为字符串。您没有指定字符编码,因此使用系统的默认字符编码,可能是也可能不是UTF-8。
  • 然后将结果(可能包含或不包含“状态”,具体取决于系统的默认字符编码)打印到out

这很可能不会做你想要的。你可以写完:

out.println("status");

如果那就是你想要的。

确切了解应从Java代码发送的响应,并编写代码以正确的格式发送响应。