我正在尝试使用Trello API的新自定义字段方法来设置卡上自定义字段的值。
我创建了一个类型编号的自定义字段。当我为自定义做出GET请求时……
实例(使用 XMLHttpRequest ) 上 Trello文档页面 是错的。您应该使用下一个示例 fetch 。
XMLHttpRequest
fetch
var url = "https://api.trello.com/1/cards/{idCard}/customField/{idCustomField}/item?token={yourToken}&key={yourKey}"; var data = {value: { number: "42" }}; fetch(url, { body: JSON.stringify(data), method: 'PUT', headers: {'content-type': 'application/json'}}) .then((resp) => resp.json()) .then((data) => console.log(JSON.stringify(data, null, 2))) .catch((err) => console.log(JSON.stringify(err, null, 2)))
这个例子有效。在尝试这个之后,我修改了 XMLHttpRequest 版本,它也有效。
var data = null; var xhr = new XMLHttpRequest(); xhr.addEventListener("readystatechange", function () { if (this.readyState === this.DONE) { console.log(this.responseText); } }); data = {value: { number: "3" }}; //added var json = JSON.stringify(data); //added xhr.open("PUT", 'https://api.trello.com/1/cards/{idCard}/customField/{idCustomField}/item?key={yourkey}&token={yourtoken}'); xhr.setRequestHeader('Content-type','application/json'); //added xhr.send(json); //modified
关键是你应该1)设置请求 Content-type 标题为 application/json 并且2)通过 value 通过JSON对象体。
Content-type
application/json
value
我试图在文档上编辑实例,但我不可能。我希望他们能尽快解决。