0

I'm a beginner with using Ajax Call and Spring server. I wanted to send some JSON format data from html to the Spring server. Here is my Ajax Call code:

function sendToServer(){
$.ajax({
    type: "POST",
    url:"/maricobalagi/getJsonByMap",
    dataType: "json",
    data:"{formdata}",
    //success and error message
    success: function() {
         alert("The page has been successfully loaded");
      },

     error: function() {
        alert("An error occurred");
      },
});
}

But i have an issue with the Ajax Call URL, it couldn't find the "/getJsonByMap" value. There is an error message "POST http://localhost:8092/maricobalagi/getJsonByMap 404 (Not Found)" in the console log. And here is my server code:

public class Controller{
@RequestMapping(value = "/getJsonByMap" , method = RequestMethod.POST)
public @ResponseBody Map post(@RequestBody final Map JSONmap){

  //myCode

}
}

i just have web.xml, here is my web.xml code:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Generic</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

And here is my project folder: Project Folder Screenshot

Ray
  • 13
  • 1
  • 7
  • 1
    Can you please post your `web.xml` and `webApp.xml` file content here? – David R Sep 26 '16 at 06:21
  • I already edit the post and i don't have any webApp.xml – Ray Sep 26 '16 at 06:29
  • Don't you have any `` defined in your `web.xml` ? – David R Sep 26 '16 at 06:32
  • Sorry i haven't include any in my web.xml, cause there's always a "cvc-complex-type.2.4.b: The content of element 'servlet-mapping' is not complete. One of '{"http:// xmlns.jcp.org/xml/ns/javaee":servlet-name}' is expected." error message. – Ray Sep 26 '16 at 06:41

1 Answers1

1

Annotate your controller class with @RestController and also return something in the method.