2016-11-25 80 views
1

我得到一個 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.的JQuery/Ajax的POST調用,不支持的媒體類型

奇怪的是,如果我看POST調用螢火蟲,他們表示不會全成,但如果我做了一個「重發」,他們被髮送並且回答被檢索。

我已經嘗試接受的答案有:jquery ajax rest call - Unsupported Media Type - 我沒有工作。

-edit:如果有幫助,我使用莫西。

   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 
        }, 

       }); 

這是我的模型:

@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; 
} 

} 

,這是我的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"); 
     } 
    } 

回答

0

刪除字符集爲JSON不支持的字符集也設置接受爲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 
         }, 

        }); 
+0

對不起,也沒有改變任何東西。 – User9123

0

嘗試修改此:

data: JSON.stringify(request),

這樣:

data: request,

+0

這不起作用。 – User9123

0
$.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' 
     }) 

這個工作對我們有gradle這個服務器以外POST請求媒體消費型應用程序json,選項belo w也使用相同的gradle設置

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