无法使用来自应用程序的REST资源 - 415响应不是JSON格式

时间:2012-06-10 16:02:12

标签: jquery json ajax rest jira

我正在尝试从独立的OpenSocial(Atlassian Jira)小工具中使用以下REST资源。

我可以使用以下网址通过浏览器查询资源:

https://jira.atlassian.com/rest/api/latest/issue/JRA-9

我可以从我的开发机器发出相同的请求。它对我来说当然看起来像JSON ......但是,当我从我的小工具查询时,我得到了

GET 10.0.15.10:2990/jira/rest/api/latest/issue/CRD-1  415 Unsupported Media Type (32ms)

响应以HTML格式返回。这是tomcat的响应主体:

<html><head><title>Apache Tomcat/6.0.20 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 415 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0.20</h3></body></html>

这是我的AJAX调用(它基本上是JQuery包含在Atlassian gadget JavaScript Framework中):

 args: [{
         key: "issueData",
         ajaxOptions: function() {
             return {
                 url: "/rest/api/latest/issue/CRDTRK-1"
             };
         }
  }]

我尝试将dataType: "json"添加到a​​jax请求但无济于事。我可能错过了一些简单的东西。

以下是请求/响应标头:

Response Headers
Content-Length  1051
Content-Type    text/html;charset=utf-8
Date    Sun, 10 Jun 2012 15:53:06 GMT
Server  Apache-Coyote/1.1
X-AREQUESTID    1013x5993x1
X-ASESSIONID    1bi5et0
X-AUSERNAME admin
X-Seraph-LoginReason    OK

Request Headers
Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-gb,en;q=0.5
Connection  keep-alive
Content-Type    application/x-www-form-urlencoded
Cookie  atlassian.xsrf.token=BP8Q-WXN6-SKX3-    
DNT 1
Host    10.0.15.10:2990
Referer http://10.0.15.10:2990/jira/secure/Dashboard.jspa
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0
X-Requested-With    XMLHttpRequest

2 个答案:

答案 0 :(得分:6)

您的小工具规范文件需要在ajax选项中发送contentType请求标头:

args: [{
    key: "issueData",
    ajaxOptions: function() {
        return {
            contentType: 'application/json',
            url: "/rest/api/2.0.alpha1/issue/CRDTRK-1"
        };
    },
}]

检查HTTP请求中的请求标头。 应该是:Content-Type: application/json 而不是:Content-Type application/x-www-form-urlencoded

答案 1 :(得分:0)

尝试使用getJSON并输出callBack =? 在哪里:

$.getJSON('https://jira.atlassian.com/rest/api/latest/issue/JRA-9jsoncallback=?', function(data){ $.each(data, function(){ alert(this.expand); }); });