2015-10-16 27 views
0

即使我發現了一些文章simliar我的問題沒有答案解決我的問題一個問題Github上:POST使用鐵AJAX

我想每一個(annonymous)允許用戶張貼問題Github上。正如我已經開發使用聚合物和Webcomponents我想做到這一點使用<iron-ajax>所以我有<iron-ajax>元素:

<iron-ajax 
    id="githubIssues" 
    url="https://api.github.com/repos/IchordeDionysos/social-contacts/issues" 
    method="POST" 
    params='{"access_token": "efd925cc3c8d593b720f0d6a88f3c36f593e063a"}' 
    body="[[params]]" 
    verbose 
    handle-as="json" 
    on-response="showSuccess"> 

</iron-ajax> 

我定義params屬性是這樣的:

params: { 
    notify: true, 
    type: Object, 
    value: { 
    "title": "", 
    "body": "", 
    "assignee": "IchordeDionysos", 
    "labels": ["0 - Backlog"] 
    } 
} 

最後我有一個按鈕哪個電話是一個功能張貼的問題,在這個功能我檢查serveral <paper-checkboxes>如果他們被檢查,如果我推進更多的標籤的參數:

submitIssue: function() { 
    if (this.$.bug.checked) { 
     this.push('params.labels', 'bug'); 
    }; 
    if (this.$.help.checked) { 
     this.push('params.labels', 'help'); 
    }; 
    if (this.$.question.checked) { 
     this.push('params.labels', 'question'); 
    }; 
    if (this.$.feature.checked) { 
     this.push('params.labels', 'feature'); 
    }; 
    if (this.$.enhancement.checked) { 
     this.push('params.labels', 'enhancement'); 
    }; 
    if (this.$.design.checked) { 
     this.push('params.labels', 'design'); 
    }; 
    console.log(this.params); 
    this.$.githubIssues.generateRequest(); 
} 

但是當我試圖發佈問題時,我得到一個400 (Bad Request)

我該如何解決這個問題以及我必須授予我的令牌的範圍?

編輯:這是我的請求頭和身體看起來怎麼樣:http://requestb.in/11y0i0x1?inspect

編輯:[對象對象]發送到身體看起來是這樣的:

{title: "dsggsdf", 
body: "sdfgsdfsdf", 
assignee: "IchordeDionysos", 
labels: Array[3]} 

和標籤陣列:

labels: Array[3] 
    0: "0 - Backlog" 
    1: "help" 
    2: "question" 

當我登錄對象到Chrome控制檯

+1

400狀態碼並不表明範圍的問題。您可以改爲向請求bin(http://requestb.in/)發出請求,而不是調用GitHub API,然後在這裏發佈請求標頭和主體的外觀如何?這應該有助於搞清楚問題 –

+0

我已經發布了一個鏈接requestb.in –

+1

謝謝。正如你所看到的 - 原始體是空的,內容長度是0.這表明你沒有正確發送正文。這可能是API響應400的原因。 –

回答

0

您必須添加屬性contentType="application/json"

像這樣:

<iron-ajax 
    id="githubIssues" 
    url="https://api.github.com/repos/IchordeDionysos/social-contacts/issues" 
    method="POST" 
    params='{"access_token": "efd925cc3c8d593b720f0d6a88f3c36f593e063a"}' 
    body="[[params]]" 
    verbose 
    handle-as="json" 
    on-response="showSuccess" 
    contentType="application/json">  
</iron-ajax> 

我希望這個作品;)

0

只需將屬性content-type="application/json"添加到您的iron-ajax屬性即可!

如果沒有content-type - 屬性ajax會將body - 屬性中提供的數據作爲字符串發送,而不是您必須發送到Github的json對象!