简体   繁体   中英

400 bad request while sending data with ajax in spring framework

Trying to pass a JavaScript object to Spring MVC. I am getting 400 bad request error. Tried all different approaches but still no luck. I have attached Controller, the object that I am passing in JS and Java and the response. Would appreciate any help. When I change @RequestBody to @ModelAttribute I receive the parameters in the controllers but the integers are zero and the arrays are 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 parameters][ https://i.stack.imgur.com/2cxde.png]

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

I'm not entirely sure, but it just doesn't seem the best idea to combine a GET request and try to pass an object, that is why you see your searchController JS object at the url, in the second picture.

The following question has a more thorough explanation.

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

Alternatively if you still want to keep it as GET for your reasons, you should pass your object as parameters in the URL and use @RequestParam in your controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM