Dwhitz
Dwhitz

Reputation: 1270

Send Xml with AJAX, JAVA WS-RS @Post

I must send an xml via Ajax, with a POST HTTP call.

I don't know how to format the text, and if it is necessary such as JSON.parse(). I tried to put my xml example into a js var. I think it's incorrect, but I don't know how to do it.

Can someone help me?

This is my jsp page which I run with tomcat server for making the call :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Send jSon - put method</title>

    </head>
    <body>
    <script>
    var peopleXml = "<person><address>prova 56</address><name>prova 1</name> <surname>prova cognome</surname></person>";
    function SaveObjectXML(){
        $.ajax({
            type: "POST",
            url: "http://localhost:8080/HibernateTutorialWeb/rest/person/postXml",
            data: peopleXML,
            contentType: "application/json; charset=utf-8",
            dataType: "xml",
            success: function(data){alert(data);},
            failure: function(errMsg) {
                alert(errMsg);
            }
      });
    }
    </script>

    <br><h2>XML</h2>

    <br><input type="button" onclick="SaveObjectXML()" value="Inserisci una persona"/>

    <div id = "footer"> 
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js">
        </script>
    </div>

    </body>
</html>

Upvotes: 0

Views: 156

Answers (1)

pessoadev
pessoadev

Reputation: 74

Are you try to send a json or xml?

contentType: "application/json; charset=utf-8",

you should try change it to: contentType: "application/xml; charset=utf-8",

Upvotes: 2

Related Questions