POST 使用 iron-ajax Github 的问题
POST an Issue to Github using iron-ajax
即使我找到了一些与我的问题相似的文章,也没有答案解决了我的问题:
我希望每个(匿名)用户允许 post 发布到 Github。因为我已经使用 Polymer 和 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: {
notify: true,
type: Object,
value: {
"title": "",
"body": "",
"assignee": "IchordeDionysos",
"labels": ["0 - Backlog"]
}
}
最后我有一个按钮调用了一个函数来解决 post 这个问题,在这个函数中我检查了几个 <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();
}
但是当我尝试 post 这个问题时,我得到了 400 (Bad Request)
我该如何解决这个问题以及我必须授予我的令牌哪些范围?
编辑:这是我的请求 header 和 body 的样子:http://requestb.in/11y0i0x1?inspect
编辑:[object Object] 发送到 body 看起来像这样:
{title: "dsggsdf",
body: "sdfgsdfsdf",
assignee: "IchordeDionysos",
labels: Array[3]}
和标签数组:
labels: Array[3]
0: "0 - Backlog"
1: "help"
2: "question"
当我将 Object 记录到 Chrome 控制台时
只需将属性 content-type="application/json"
添加到您的 iron-ajax 属性中即可!
没有 content-type
- 属性 ajax 会将 body
- 属性中提供的数据作为字符串发送,而不是作为 json 对象发送post 到 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>
我希望这能奏效 ;)
即使我找到了一些与我的问题相似的文章,也没有答案解决了我的问题:
我希望每个(匿名)用户允许 post 发布到 Github。因为我已经使用 Polymer 和 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: {
notify: true,
type: Object,
value: {
"title": "",
"body": "",
"assignee": "IchordeDionysos",
"labels": ["0 - Backlog"]
}
}
最后我有一个按钮调用了一个函数来解决 post 这个问题,在这个函数中我检查了几个 <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();
}
但是当我尝试 post 这个问题时,我得到了 400 (Bad Request)
我该如何解决这个问题以及我必须授予我的令牌哪些范围?
编辑:这是我的请求 header 和 body 的样子:http://requestb.in/11y0i0x1?inspect
编辑:[object Object] 发送到 body 看起来像这样:
{title: "dsggsdf",
body: "sdfgsdfsdf",
assignee: "IchordeDionysos",
labels: Array[3]}
和标签数组:
labels: Array[3]
0: "0 - Backlog"
1: "help"
2: "question"
当我将 Object 记录到 Chrome 控制台时
只需将属性 content-type="application/json"
添加到您的 iron-ajax 属性中即可!
没有 content-type
- 属性 ajax 会将 body
- 属性中提供的数据作为字符串发送,而不是作为 json 对象发送post 到 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>
我希望这能奏效 ;)