0

I have an ajax request, and I would like to use the data what I get back from server side. I know the frontend code, but I don't know the backend. Please help me guys! My frontend code is here:

    <script>
            function login() {
               alert("alma");
               var sendInfo = {
               user: document.getElementById("username"),
               passwd: document.getElementById("password")
               };
       $.ajax({
        type: 'POST',
        url: 'HalloWorld',
        data: sendInfo, 
        dataType: 'json',
        contentType: 'application/.json',
        success: function(data) { 
                    //here I want to use my data from sever side
                    document.getElementById("newdata").innerHTML=data;
                 },
        error: function(jqXHR, textStatus, errorThrown) {
                    alert("error");
               }
    });

}
        </script>

    Username: <input type="text" id="username"/> <br/>
    Password: <input type="password" id="password"/> <br/>
    <input type="button" value="Login" onclick="login()"/>
    <div id="newdata"></div>
Orshee
  • 1
  • 4
    you are sending and receiving json. Your servlet should create a json object and write it back – Scary Wombat Nov 17 '16 at 07:54
  • In your ajax success handler, you're using the response to directly write to `innerHTML`. Based on that, it doesn't sound like you want to send back an object, it sounds like you want to send back the HTML representation of an object. So where are you stuck? (Also, if that's true, your `dataType` is wrong.) – T.J. Crowder Nov 17 '16 at 07:56
  • Please mention which framework are you using in server side. Is it only java servlet or framework like Spring? – timekeeper Nov 17 '16 at 08:01
  • Only java servlet. So my problem is, that I cannot get back for example a simple string from the servlet. I need an example servlet code how to send back any data to client HTML. – Orshee Nov 18 '16 at 06:41

0 Answers0