简体   繁体   中英

AJAX post request not working, working fine with postman post request

Been trying to solve this issue for several days but to no avail. When I try to post to my rest I get my error message from error: function()

This is my ajax code that executes when I press the send button in my html site

var bord = {
           plasser: $plasser.val()
       };


 $.ajax({
      url: 'rest/bord',
      type: 'POST',
      data: bord,
      dataType: 'JSON',
      success: function(nyttBord){
          alert("sending successful");
          $bords.append("<li>id: "+ nyttBord.id +", plasser: "+ nyttBord.plasser + "</li>");
      },
      error: function(){
          alert("sending failed" + " plasser: " + bord.plasser);
      }
   });

My "BordResource" post method:

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Bord addBord(Bord bord){
        return bordService.settInnBord(bord);
    }

I have tried sending post requests with postman with just the and it works just fine, so I'm kind of confused as to why this ajax function is getting an error

your missing contentType: "application/json", and change dataType: 'JSON', into dataType: 'json', , so add it into your code like this

   type: 'POST',
  data: JSON.stringify(bord),
  contentType: "application/json",
  dataType: 'json',

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