java 如何在 jQuery 中从客户端向服务器发送 XML 文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3571768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send a XML doc to server from client in jQuery
提问by Zeck
I'm trying to send XML doc to server from client. But when server get a XML doc. It's always empty. Here is my jquery function. It's send XML to server:
我正在尝试从客户端向服务器发送 XML 文档。但是当服务器得到一个 XML 文档时。它总是空的。这是我的 jquery 函数。它将 XML 发送到服务器:
var str = '<?xml version="1.0" encoding="UTF-8"?><foo><bar>Hello World</bar></foo>';
var xmlData = strToXml(str); // convert string to xml
console.log($.isXMLDoc(xmlData)); // return true
$.ajax({
url: 'foo.bar'
, processData: false
, data: xmlData
, success: function(response){
console.log(response);
}
, error: function(response) {
console.log(response);
}
});
And server side code. It's recieve a xml doc.
和服务器端代码。它收到一个 xml 文档。
try {
HttpServletRequest request = ServletActionContext.getRequest();
InputStream is = request.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line = "";
System.out.println(reader.read()); // return -1
while((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
Can you guys put some working example? And thank you for any advice and post.
你们能举一些有效的例子吗?并感谢您的任何建议和帖子。
回答by naikus
You are missing the "type" property in your ajax request. The default value if you don't provide it is GET.
您在 ajax 请求中缺少“类型”属性。如果您不提供,则默认值为 GET。
Also there's no need to convert your data to XML Dom when you are sending it over the wire, unless you want to do something with it on client side:
此外,当您通过网络发送数据时,无需将数据转换为 XML Dom,除非您想在客户端对其进行处理:
function sendXml() {
var str = '<?xml version="1.0" encoding="UTF-8"?><foo><bar>Hello World</bar></foo>';
// var xmlData = strToXml(str); // no need for this unless you want to use it
// on client side
// console.log($.isXMLDoc(xmlData));
$.ajax({
url: 'test.jsp',
processData: false,
type: "POST", // type should be POST
data: str, // send the string directly
success: function(response){
alert(response);
},
error: function(response) {
alert(response);
}
});
}
回答by Aaron Saunders
$.ajax({
type: "POST",
url: 'foo.bar',
processData: false,
data: xmlData,
success: function(response){
console.log(response);
},
error: function(response) {
console.log(response);
}
});
回答by Pratik Bhatt
Besides the point of missing the type in your ajax request. Have you tried simply using
除了缺少 ajax 请求中的类型之外。你有没有试过简单地使用
String xmlData = request.getParameter("data");
This would be the simplest way to access the "data" parameter from the post request.
这将是从发布请求访问“数据”参数的最简单方法。
As from the JavaDoc, getInputStream is to be used with binary data; to handle text data use getReader(), which returns a bufferedReader to you.
从JavaDoc 开始, getInputStream 将用于二进制数据;使用 getReader() 处理文本数据,它会向您返回一个 bufferedReader。

