1

I know this may be asked before, but I have tried everything in stackoverflow and nothing has worked. I am using Spring Security so idk if this has anything to do. Having said that, the ajax POST request simply does not reach my controller, not even a error message or anything. The ajax POST request returns "sucess" but nothing happens.

I have tried (Send json with Ajax to Spring MVC) (How to pass Json object from ajax to spring mvc controller?) (Spring controller not getting JSON POST data from JS file) and a few more.

JS

function cartData(){
    //var prods = [];
    //prods = JSON.parse(localStorage.getItem("cart"));
    var thing = {id: "1",cant:"1"};
    //console.log(prods);
    $.ajax({   
        type:"POST",
        contentType: "application/json; charset=utf-8",
        url: 'cart', 
        data: thing,
        success : function(result) {
             alert('success');
        },
         error : function() {
            alert("not working");
        }
});
}

Controller

//Class
public class Thing
    {
        private String id; 
        private String cant;
        
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getCant() {
            return cant;
        }
        public void setCant(String cant) {
            this.cant = cant;
        } 
        
    }
//Controller
    @RequestMapping(value="/cart",method=RequestMethod.POST)
    public  @ResponseBody String  cart(@RequestBody Thing thing, HttpServletRequest request) {
        System.out.println("Got Inside");

        return "public/carrito";
    }
    
Frandisc
  • 11
  • 1
  • 3
  • did you try data: JSON.stringyfy(thing), ? – Prakash S Jul 16 '21 at 04:07
  • @PrakashS Yes, I tried it before and rigth now, but no changes. :( – Frandisc Jul 16 '21 at 04:09
  • data: JSON.stringify(thing), contentType: 'application/json; charset=utf-8', dataType: 'json', – Prakash S Jul 16 '21 at 04:13
  • @PrakashS if I set dataType: 'json' it return me an error (alert("not working");) which is weird. It's been 2 days with this error. So I kinda given up and I think I'll try something else. – Frandisc Jul 16 '21 at 04:17
  • check the network request and mostly it is a server parsing error. so the request is hitting the server/controller – Prakash S Jul 16 '21 at 04:22
  • 1
    @PrakashS I tried to post images of the network, but I dont have enough reputation lol. It seems to be a problem in spring security since it redirects me to the login page in the network redirects. Either way I'll check it out. Thanks a lot! – Frandisc Jul 16 '21 at 04:46
  • I will increase it by up vote. Everyone deserves help – Prakash S Jul 16 '21 at 05:50

0 Answers0