$("#submit").on('click',function () { $.ajax({ url:"http://localhost:8080/webtest/service", method:"POST", dataType:"application/json", success:function () { alert("Ajax request success"); }, error:function () { alert("look at the response headers for error!") } }); }); }); 
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Tests</title> <script src="jquery-3.1.0.js"></script> <script src = "https://code.jquery.com/jquery-1.11.3.js"></script> <script src = "https://code.jquery.com/jquery-2.1.4.js"></script> </head> <body> <form"> <label for = "username">UserName:</label> <input id = "username"><br> <label for = "email">Email:</label> <input id = "email" type="email"><br> <label for = "password">Password:</label> <input id = "password" type="password"><br> <input id = "submit" type="button" value="Send!"> </form> <script src="requests.js"></script> </body> </html> 

 @WebServlet(name = "webService" , urlPatterns = "/service") public class WebServiceDemo extends HttpServlet { response.setHeader("Access-Control-Allow-Origin","*"); response.getWriter().print("In the if else DO POST METHOD"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { **//When the server starts i got response from here!** response.getWriter().print("Hello from servlet"); } } 

Meet with Ajax requests. What is wrong with this code? When you try to submit a request for an error report! I get a refund from server 200! But the Jquery Ajax code of the error function with an alert is output! Why

enter image description here

    4 answers 4

    Ajax request is written correctly, most likely an error in the server side. The request to the server indicates that the data should be returned as JSON (application / json), but you simply return the string

    response.getWriter (). print ("In the if else DO POST METHOD") ;.

    Try to return the data as something like

    response.getWriter (). print ({"string": "In the if else DO POST METHOD"})

    And in javascript, change the success method

     success: function (responce) { alert(responce.string); } 

    You can still try to just specify the dataType: "text / plain"

     $.ajax({ url:"http://localhost:8080/webtest/service", method:"POST", dataType:"text/plain", success:function () { alert("Ajax request success"); }, error:function () { alert("look at the response headers for error!") } }); 

      I myself recently got acquainted with the question and do this:

       function loadDoc() { xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState === 4 && xmlhttp.status === 200) { processJSON(xmlhttp.responseText); } } var url = 'your url'; xmlhttp.open("GET", url, true); xmlhttp.send(); } function processJSON(json) { if (typeof JSON !== "undefined") { json = JSON.parse(json); //your code } } 

        Your mistake is here:

         dataType: "application/json", 

        First, the dataType parameter in jquery is not a mime type at all. Secondly, your service does not return json at all!

        In accordance with the documentation , you will be more suitable in your case.

         dataType: "text", 
           $.ajax({ url: 'http://google.com/', type: 'get', success: function(msg) { alert(msg); } }); 
          • I do not understand your answer! So I changed. Add code from the top. Something netak with AJAX function, what is this error? - Maks.Burkov
          • Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky