Home > Blockchain >  controller is not recognizing @requestparam from ajax
controller is not recognizing @requestparam from ajax

Time:04-09

i am trying to send json from ajax to springboot controller but getting error [org.springframework.web.bind.MissingServletRequestParameterException: Required Item[] parameter 'myJsonString[]' is not present]

below is my ajax code

$.ajax({
    type: "POST",
    url: "/DBA_TOOLS/admin/tracker",
    data: {myJsonString:myJsonString},
    dataType: "json",
    success: function (data){
        if(data.status == "SUCCESS"){
            alert("AJAX request successfully completed:" JSON.stringify(data));
        }else{
            console.log('Error Msg' JSON.stringify(data));
        }
        console.log('Error Msg' data.message '' JSON.stringify(data));
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log(jqXHR.status);
        console.log(textStatus);
        console.log(errorThrown);
    },
    complete: function() {

    }
});

below is my controller

@PostMapping(value="/tracker")
public int traker(@RequestParam(value="myJsonString[]") Item[] myJsonString){    

    System.out.println("getting somethig from tracker");
    
    int count=0;
    try{
        for(Item item : myJsonString){
           System.out.println("User  is updating item id " item.getName());
           //count = ggProcessRepository.changeAlert(row,isActive);
        }
    }catch(Exception e){
        System.out.println("Exception Occured" e);
    }
    return count;
}

i tried with requestparam required false and its let the block working but how to get the ajax data to my requestparam is the challenge for me.

in browser debug i see the form data getting send is --> myJsonString: [{"name":"product 2","price":20,"quantity":1},{"name":"product 3","price":30,"quantity":1}]

CodePudding user response:

What happens if you change it to:

traker(@RequestParam(value="myJsonString")
  • Related