Javascript 使用iron ajax将问题发布到Github
即使我发现一些文章与我的问题相似,也没有答案解决我的问题: 我希望每个(匿名)用户都能允许将问题发布到Github。由于我已经使用聚合物和网络组件进行了开发,我想使用Javascript 使用iron ajax将问题发布到Github,javascript,ajax,polymer,github-api,Javascript,Ajax,Polymer,Github Api,即使我发现一些文章与我的问题相似,也没有答案解决我的问题: 我希望每个(匿名)用户都能允许将问题发布到Github。由于我已经使用聚合物和网络组件进行了开发,我想使用进行开发,因此我有元素: <iron-ajax id="githubIssues" url="https://api.github.com/repos/IchordeDionysos/social-contacts/issues" method="POST" params='{"access_token": "
进行开发,因此我有
元素:
<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>
最后,我有一个按钮,它调用一个函数来发布问题,在这个函数中,我检查服务器
是否被选中,如果我将更多标签推到参数:
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(错误请求)
我如何解决这个问题,以及我必须授予我的令牌哪些作用域
编辑:以下是我的请求标头和正文的外观:
编辑:发送到正文的[object]如下所示:
{title: "dsggsdf",
body: "sdfgsdfsdf",
assignee: "IchordeDionysos",
labels: Array[3]}
和标签数组:
labels: Array[3]
0: "0 - Backlog"
1: "help"
2: "question"
当我将对象登录到Chrome控制台时,只需将属性
content type=“application/json”
添加到您的iron ajax属性中
如果没有
内容类型
-属性,ajax将把正文
-属性中提供的数据作为字符串发送,而不是作为json对象发送,因为您必须发送到Github 您必须添加属性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>
我希望这能奏效;) 400状态代码并不表示作用域有问题。您可以不调用githubapi,而是向请求bin()发出请求,然后在这里发布请求头和主体的确切外观吗?这应该有助于找出问题所在。我已经发布了一个到requestb.inThanks的链接。如您所见,原始正文为空,内容长度为0。这表明您没有正确发送正文。这可能就是API响应400的原因。好吧,我做了一些更改,现在我在正文部分看到我发送了一个
[object object]
,内容长度不再是0!这仍然不好——这意味着发送到GitHub API的原始字符串是[object object]
,而不是JSON对象。您似乎没有正确地将对象序列化为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>