简体   繁体   English

JQuery / Ajax POST调用,不支持的媒体类型

[英]JQuery/Ajax POST call, Unsupported Media Type

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. 我得到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. 奇怪的是,如果我查看firebug中的POST调用,则表示它们不成功,但如果我执行“重新发送”,则会发送它们并检索答案。

I already tried the accepted answer there: jquery ajax rest call - Unsupported Media Type - did not work for me. 我已经在那里尝试了接受的答案: jquery ajax rest call - 不支持的媒体类型 - 对我不起作用。

-edit: If it helps, I use Moxy. -edit:如果有帮助,我会使用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: 这是我的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");
        }
    }
$.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 除了消费媒体类型应用程序json的帖子请求之外,这对我们有一个gradle服务器,下面的选项也使用相同的gradle设置

$.ajax({
            type: 'POST',
            url: API_URL,
            contentType: 'application/json',
            data: JSON.stringify(data),
            complete: resolve
        });

Remove charset as json dont support charset also set accept as json since you are producing json for client. 删除charset,因为json不支持charset也设置accept为json,因为你正在为客户端生成json。

            $.ajax({      
                     url: "http://localhost:8080/MyProject/Rest/Path/toPost",
                     type: "POST",
                     data: JSON.stringify(request),
                     Accept : "application/json",
                     contentType: "application/json", 
                     dataType: "json",
                         success: function(resultData) {

                              //do stuff
                         },

                   });

Try changing this: 尝试改变这个:

data: JSON.stringify(request),

to this: 对此:

data: request,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jquery ajax rest 呼叫 - 不支持的媒体类型 - jquery ajax rest call - Unsupported Media Type ASP AJAX POST调用返回不支持的媒体类型 - ASP AJAX POST call returns Unsupported Media Type 400错误的请求或415不支持的媒体类型在Ajax呼叫中 - 400 Bad request or 415 Unsupported Media type in Ajax post call Ajax调用在JQuery中提供了不受支持的媒体类型,但在MVC中却没有 - Ajax call gives Unsupported Media Type in JQuery, but not in MVC 415不支持的媒体类型jQuery Ajax - 415 Unsupported Media Type jQuery Ajax 在ajax调用上获取“不受支持的媒体类型415”-Jquery,ajax - Getting an “Unsupported media type-415” on an ajax call- Jquery,ajax jQuery数据未在ajax发布请求中传递,因此获得415不支持的媒体类型状态代码 - JQuery data is not passed in the ajax post request, Hence get 415 Unsupported Media Type status code HTTP状态415 - JQUERY中使用JERSEY实现的Restful WS中的AJAX调用不支持的媒体类型 - HTTP Status 415 - Unsupported Media Type for AJAX call in JQUERY to Restful WS implemented with JERSEY jQuery不支持的媒体类型 - JQuery Unsupported Media Type ajax json发布到Spring mvc控制器“ 415不支持的媒体类型” - ajax json Post to Spring mvc Controller “415 Unsupported Media Type”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM