400 Bad Request when sending data using Ajax in Spring Framework


MJ82

Trying to pass JavaScript object to Spring MVC. I get a 400 Bad Request error. Tried all different ways but still no luck. I've attached the Controller, the objects I'm passing in JS and Java and the response. I will really appreciate. When I change the @RequestBody to @ModelAttribute, I receive the parameter in the controller, but the integer is zero and the array is empty.

controller

    @RequestMapping(value="/getInstrumentsByTypeMakeModel" , method=RequestMethod.GET)
    public @ResponseBody List<Instrument> getInstrumentsByTypeMakeModel(@RequestBody SearchInstrument search) {
     // do Something and create searchResult;   
    return searchResult;
}

javascript

var searchInstrument = {

    operator: siteOperator,
    companyId:loggedInUserCompanyId ,
    isServiceProvider:isServiceProvider ,
    types:selectedType,
      makes:selectedMake,
      models:selectedModel,
      typeOthers:selectedTypeOther,
      makeOthers:selectedMakeOther,
      modelOthers:selectedModelOther

    };



     jQuery.ajax({
         url:server_name+ "/getInstrumentsByTypeMakeModel",
         type: "GET",
        dataType: "JSON",
        data:JSON.stringify(searchInstrument),   
         contentType: 'application/json; charset=utf-8',
         success: function(resultData) {
             console.log(resultData);
            },
             error: function(jqXHR, status) {
                 // error handler

            }
        }); 

SearchInstrument class

public class SearchInstrument  {
    public SearchInstrument(){

    }

        public SearchInstrument(int operator,int companyId, boolean isServiceProvider,
                List<Integer>  types,List<Integer>  makes,  List<Integer>  models,
                List<String>  typeOthers,List<String>  makeOthers,List<String>  modelOthers
                ){

this.operator=operator;
this.companyId=companyId;
this.types=types;
this.makes=makes;
this.models=models;
this.typeOthers=typeOthers;
this.makeOthers=makeOthers;
this.modelOthers=modelOthers;
        }
        private int operator;
        private int companyId;
        private boolean isServiceProvider;
        private List<Integer>  types=new ArrayList<Integer> ();
        private List<Integer>  makes=new ArrayList<Integer> ();
        private List<Integer>  models=new ArrayList<Integer> ();    
        private List<String>  typeOthers=new ArrayList<String> ();
        private List<String>  makeOthers=new ArrayList<String> ();
        private List<String>  modelOthers=new ArrayList<String> ();

//getters and setters       

    }

[this is the parameter] [ https://i.stack.imgur.com/2cxde.png]

[This is the URL:] [ https://i.stack.imgur.com/BPMaS.png]

Christos Karapapas:

I'm not entirely sure, but combining GET requests and trying to pass an object doesn't seem like the best idea, which is why you see the JS object searchControlleron the URL in the second picture .

The following questions are explained in more detail.

I think you should try converting it to a POST request.

Or if for some reason you still want to keep it as GET, you should pass the object as a parameter in the URL and use @RequestParam in the controller.

Related


400 Bad Request when sending data using Ajax in Spring Framework

MJ82: Trying to pass JavaScript object to Spring MVC. I get a 400 Bad Request error. Tried all different ways but still no luck. I've attached the Controller, the objects I'm passing in JS and Java and the response. I will really appreciate. When I change the

400 Bad Request when sending data using Ajax in Spring Framework

MJ82: Trying to pass JavaScript object to Spring MVC. I get a 400 Bad Request error. Tried all different ways but still no luck. I've attached the Controller, the objects I'm passing in JS and Java and the response. I will really appreciate. When I change the

400 Bad Request when sending data using Ajax in Spring Framework

MJ82 Trying to pass JavaScript object to Spring MVC. I get a 400 Bad Request error. Tried all different ways but still no luck. I've attached the Controller, the objects I'm passing in JS and Java and the response. I will really appreciate. When I change the @

400 Bad Request when using Spring Boot

Steve 450 I'm using Spring Boot to send an Http POST request to my RESTful API build and get a "400 Bad Request" response. My POST request is made by Postman, sent to http://localhost:8080/executebash with the body { "filename": "blaba" } I want to pass

400 Bad Request on Spring Ajax call

Davis I am getting an error in Spring Ajax call. I know it's some syntax error, but can't find it. Can someone help? script: $.ajax({ type : 'POST', contentType : 'application/json; charset=utf-8', url: 'saveCommittedAmount.html?investm

Spring MVC Ajax 400 Bad Request

username I get "400: Bad Request" when triggering an Ajax request, which is the "top right" login. I don't know why In firebug, I can see that the request url is correct, but "400: Bad Request" is returned. I can't debug as it seems the client hasn't successfu

Spring MVC 400 Bad Request Ajax

ep I keep getting 400 Bad Request on Ajax requests. I don't know what's wrong with this. I am using: <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.12</version> </dependency> <depende

400 Bad Request on Spring Ajax call

Davis I am getting an error in Spring Ajax call. I know it's some syntax error, but can't find it. Can someone help? script: $.ajax({ type : 'POST', contentType : 'application/json; charset=utf-8', url: 'saveCommittedAmount.html?investm