Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery/Ajax POST call, Unsupported Media Type

Tags:

I get an Unsupported Media Type - The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

Strangely, if I look at the POST calls in firebug, they are indicated as not successfull, but if I do a "resend", they are sent and an answer is retrieved.

I already tried the accepted answer there: jquery ajax rest call - Unsupported Media Type - did not work for me.

-edit: If it helps, I use Moxy.

                var sent = "This is a test request.";
                var add = "testing..";
                var request = { sentence: sent, addition: add };
                $.ajax({

                 url: "http://localhost:8080/MyProject/Rest/Path/toPost",
                 type: "POST",
                 data: JSON.stringify(request),
                 contentType: "application/json; charset=utf-8", 
                 dataType: "json",
                     success: function(resultData) {

                          //do stuff
                     },

               });

This is my model:

@XmlRootElement(name = "request")
public class Request {

private String sentence; 
private String addition;

public Request() {
    this.sentence = "default";
    this.addition = "default";
}

public Request(String sentence, String add) {
    this.sentence = sentence; 
    this.addition = add;
}

   public String getSentence() {
       return sentence;
   }

   public String getAddition() {
       return addition;
   }

   public void setSentence(String sentence) {
       this.sentence = sentence;
   }
   public void setAddition(String addition) {
       this.addition = addition;
   }

}


@XmlRootElement(name = "answer")
public class Answer {

private ArrayList<String> lsit;
private String info;

public Answer() {
    this.list = new ArrayList<String>();
    this.info = "Good";
}


public Answer(ArrayList<String> list, String info) {
    this.list = list;
    thisinfo = info;
}


public void setInfo(String info) {
    this.info = info;
}
public String getInfo() {
    return info;
}

public ArrayList<String> getList() {
    return list;
}
public void setList(ArrayList<String> list) {
    this.list = list;
}

}

And this is my Servlet:

    @Path("/Path")
    public class TestServlet {

        @Path("/toPost")
        @POST
        @Consumes({MediaType.APPLICATION_JSON})
        @Produces({MediaType.APPLICATION_JSON})
        public Answer consume(Request request) {
            ArrayList<String> res = new ArrayList<String>();
            res.add(request.getSentence());
            return new Answer(res, "Good");
        }
    }
like image 319
User9123 Avatar asked Nov 25 '16 11:11

User9123


1 Answers

$.ajax({
            beforeSend: function(xhrObj){
                xhrObj.setRequestHeader("Content-Type","application/json");
                xhrObj.setRequestHeader("Accept","application/json");
            },
            type: "POST",
            url: API_URL,
            data: JSON.stringify(data),
            contentType: 'application/json',
            success: resolve,
            dataType: 'json'
        })

This worked for us with a gradle server excepting a post request consuming media type application json, the option below also worked with the same gradle setting

$.ajax({
            type: 'POST',
            url: API_URL,
            contentType: 'application/json',
            data: JSON.stringify(data),
            complete: resolve
        });
like image 57
lsharir Avatar answered Sep 23 '22 16:09

lsharir