Ajax request is not calling Spring boot controller

spring boot ajax post example
return list from spring controller to ajax
ajax call from jsp to spring controller example
jquery ajax spring mvc crud example
spring mvc jquery ajax json example
how to send ajax request to spring controller
ajax call in spring mvc
spring mvc jquery ajax form submit example

I'm trying to call my Spring controller using Ajax and submitting a form.

Function always retrieves the error window. I tried changing the URL parameter to "/profile", "profile" or "PrivateAreaController/profile", but I keep getting the same error.

My main.js file and controller are placed in the following order:

-->Mainfolder
    -->src
       -->java
          -->controller
              -->PrivateAreaController.java
       -->resources
           -->static
              -->js
                 -->main.js

My controller is called PrivateAreaController

Ajax Code

$('#sampleForm').submit(
    function(event) {
        var firstname = $('#firstname').val();
        var lastname = $('#lastname').val();
        var data = 'firstname='
            + encodeURIComponent(firstname)
            + '&lastname='
            + encodeURIComponent(lastname);
        $.ajax({
            type : "POST",
            dataType: "json",
            url : '@Url.Action("callingcontroller","PrivateAreaController")',
            contentType: "application/json; charset=utf-8",
            data : data,
            success : function(response) {
                alert( response );
            },
            error : function() {
                alert("not working");
            }
        });
        return false;
    }); 

Spring code

@RequestMapping(value = "/profile", method = RequestMethod.POST)
        public @ResponseBody
        String processAJAXRequest(
                @RequestParam("firstname") String firstname,
                @RequestParam("lastname") String lastname   ) {
            String response = "";

            System.out.println("working");
            return response;
        }

HTML form

<form id="sampleForm" method="post" action="/profile">
         <input type="text" name="firstname" id="firstname"/>
         <input type="text" name="lastname" id="lastname"/>
         <button type="submit" name="submit">Submit</button>
</form>

EDIT:

I found the answer.. i needed to add

@CrossOrigin(origins = "http://localhost:8080")

before the @RequesMapping parameter and change the url parameter of the ajax call to url: 'http://localhost:8080/(your requestmapping parameter)

I found the answer.. i needed to add

@CrossOrigin(origins = "http://localhost:8080")

before the @RequesMapping parameter and change the url parameter of the ajax call to url: 'http://localhost:8080/(your requestmapping parameter)

Spring Boot Ajax example – Mkyong.com, js /api/search post ajax is called which lookup for SearchController as you will drill down and u will go to SearchController you will find that this is  You will learn how to make jQuery Ajax GET and POST calls to controller methods. When we use jQuery Ajax to access server (controller’s method) without reloading the web page we have two choices on how to pass the information for the request to the server (controller’s method).

What is error you are getting, Press F12 and go to Network tab and the press submit button now see the url, try adding url:"../your service URL..

How to use AJAX and jQuery in Spring Web MVC (.jsp) Application , It means, i want to display the new value of message received from my controller after an ajax call, refreshing only the specific div (not the page). Thanks! Reply. Ajax request is not calling Spring boot controller. Ask Question Asked 1 year, I'm trying to call my Spring controller using Ajax and submitting a form.

Well. I never seen this part before.

@Url.Action("callingcontroller","PrivateAreaController")

I normally do like as below:

 $.ajax({
        type : "POST",
        dataType: "json",
        url : '/profile',
        contentType: "application/json; charset=utf-8",
        data : data,
        success : function(response) {
            alert( response );
        },
        error : function() {
            alert("not working");
        }
 });

But it can have a problem with the contextPath. So, What I do is adding 'request.getContextPath()/' as below:

     $.ajax({
        type : "POST",
        dataType: "json",
        url : '${request.getContextPath()}/profile',
        contentType: "application/json; charset=utf-8",
        data : data,
        success : function(response) {
            alert( response );
        },
        error : function() {
            alert("not working");
        }
    });

The contextPath has the URL of your start page. For instance, 'www.google.com' or 'www.naver.com'

But in general, Since I personally use the contextPath a lot, I make a value and keep it.

var context = ${request.getContextPath()};

Then, your code will look neat and easy to reuse.

And also you can figure it out with the error attribute.

        error : function(err) {
            console.log("not working. ERROR: "+JSON.stringify(err));
        }

I hope this works out.

Getting Started, First, you will create the jQuery controller module that will consume the REST method completes, passing the data result from the completed AJAX request. The first case is when you already have an html form in your page and you don’t want to pass the form data as is to the back-end, e.g. a typical scenario is that your servlet expects a dynamic field other than the form fields filled by the end-user. To resolve this situation, the expected dynamic field is normally defined as a hidden form

Spring MVC, If an application context hierarchy is not required, applications can return all For security reasons, browsers prohibit AJAX calls to resources outside the  How to use DataTable with Spring Boot for Server Side Processing Pavan September 14, 2018 Java 84 Comments If you have worked on web development projects that deals with thousands of records getting displayed on page then you must have worked with or heard of Datatables .

Spring Framework + Jquery Ajax Request and Json Response, Spring MVC 3 and JQuery is one of the great combination to perform the ajax request and response. In this tutorial we are Spring Mvc Controller with Ajax Request Example It is also work in jquery datatable ajax call? This is the first presentation of the "Web API with AJAX" article series. This article provides a basic understanding of the Web API and concepts of AJAX in web applications. We can say that the Web API is the latest communication technology of the Microsoft platform.

How to fix Spring boot controller getting null data from AJAX , Jquery Ajax POST-GET Nested Objects to SpringBoot server Your browser does not currently recognize any of the video formats available. Click here to visit our Create RestController for POST & GET requests – Create  Lifecycle of a Request-Response Process for a Spring REST API The steps involved in the lifecycle of a request process and how the request is mapped to the appropriate controller method and then

Comments
  • What's the error? The content of data doesn't look like valid JSON.