如何将XML文档从客户端的jQuery发送到服务器 [英] How to send a XML doc to server from client in jQuery

查看:101
本文介绍了如何将XML文档从客户端的jQuery发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从客户端发送XML文档到服务器。但是,当服务器得到一个XML文档。它总是空的。这是我的jQuery功能。这是发送XML服务器:

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:

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);
        }
    });

和服务器端code。这是免费获赠XML文档。

And server side code. It's recieve a xml doc.

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.

推荐答案

你缺少你的Ajax请求的类型属性。如果你不为它的默认值是GET。

You are missing the "type" property in your ajax request. The default value if you don't provide it is GET.

也没有必要将数据转换为XML DOM,当你发送过来的线,除非你想用它做的东西在客户端:

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:

     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);
           }
        });
     }

这篇关于如何将XML文档从客户端的jQuery发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆