0

I am stumped with a simple subject. I am trying to populate and send a custom java object as data for my controller using ajax. Would you have a pointer as to why it fails with a 400 error (Bad Request)?

Here is the custom object:

import org.springframework.stereotype.Component;

import lombok.Getter;
import lombok.Setter;

@Component
@Getter
@Setter
public class XpTableCriteria {

    String globalSearch;   
    
    String columnsSearchCriteria;
}

And here is the script in my html page :

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div id='result'>

</div>
    <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script type="text/javascript">

$(document).ready(
        function() {    
            getTableData();
        }
);

function getTableData(){    
    
    

    var xpTableCriteria = {globalSearch: "test", columnsSearchCriteria: "test2"};
    var parameter = {'xpTableCriteria': xpTableCriteria};
    
    $.ajax({
        type : 'GET',
        data : {parameter},
        dataType: 'json',
        url : 'all',
        success : function(result) {
            alert("OK");
            
        },
      error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.responseText);
        }
    });     
}
</script>
</body>
</html>

The controller itself does not seem to be relevant, as the bug occurs before entering it. Here it is included:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.domain.XpTableCriteria;



@RestController
public class Controller {
    
    @GetMapping(value = "/all")
    @ResponseBody
    public XpTableCriteria getAll(@RequestParam XpTableCriteria xpTableCriteria) {

        return null;
    }

}

Here is the pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>ajaxobject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ajaxobject</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>                       
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Thank you very much in advance!

Tharsant
  • 1
  • 4
  • What happens when you change `data: {parameter}` to `data: xpTableCriteria`? – aksappy Sep 01 '21 at 14:50
  • Cloud you please add more detail into your question? Like error message and the controller definition. – Luan Kevin Ferreira Sep 01 '21 at 14:54
  • 400 also comes with a description, and you didn't show your controller. – chrylis -cautiouslyoptimistic- Sep 01 '21 at 14:58
  • I edited the subject to include the controller. The error message is sadly quite short ({"timestamp":"2021-09-02T07:03:54.017+00:00","status":400,"error":"Bad Request","path":"/all"}). Switching from data:{parameter} to data:xpTableCriteria does not change the result. – Tharsant Sep 02 '21 at 07:04
  • have a look at this link [https://stackoverflow.com/questions/16017081/getting-400-bad-request-error-in-jquery-ajax-post] it might be solve your problem – Bhanu Pratap Sep 02 '21 at 11:56
  • can you please add / before all in url. and you can use debugger to check line by line code in javascript. it will help to you for the solving your problem – Rajesh Patel Sep 02 '21 at 12:01

0 Answers0