gpt4 book ai didi

jquery - 如何使用 jQuery 及其 REST API 在 Confluence 中创建新页面?

转载 作者:行者123 更新时间:2023-12-01 06:45:51 24 4
gpt4 key购买 nike

我一直在寻找一个纯 HTML 和 jQuery 示例,说明如何使用 Confluence 的 REST API 创建页面。我找到了以下示例,但它们都不适合我:

示例 A

var username = "admin";
var password = "admin";
var jsondata = {"type":"page",
"title":"My Test Page",
"space":{"key":"TST"},
"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}};

$.ajax
({
type: "POST",
url: "http://localhost:8080/confluence/rest/api/content/",
contentType:"application/json; charset=utf-8",
dataType: "json",
async: false,
headers: {
"Authorization": "Basic " + btoa(username+ ":" + password)
},
data: JSON.stringify(jsondata),
success: function (){
console.log('Page saved!');
},
error : function(xhr, errorText){
console.log('Error '+ xhr.responseText);
}
});

示例 B

var jsondata = {"type":"page",
"title":"My Test Page",
"space":{"key":"TEST"},
"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}

$.ajax
({
type: "POST",
url: "http://localhost:8080/confluence/rest/api/content/",
contentType:"application/json; charset=utf-8",
dataType: "json",
async: false,
beforeSend: function (xhr){
xhr.setRequestHeader('Authorization', "Basic YWRtaW46YWRtaW4= " );
},
data: JSON.stringify(jsondata),
success: function (){
console.log('Page saved!');
}
});

我的代码

var data = {
type: "page",
title: "New Page",
space: {
key: "TST"
},
body: {
storage: {
value: "<p>This is a new page</p>",
representation: "storage"
}
}
};

var link = 'http://confluence.mysite.com:80/rest/api/content/';

$.ajax({
url: link,
type: "POST",
dataType: "json",
crossDomain: true,
username: user.username,
password: user.password,
data: JSON.stringify(data),
contentType: "application/json",
success: function (result) {
alert('Data saved!');
},
error: function (xhr, status, error) {
alert('Status: ' + status + '\n' +
'Error: ' + error);
}
});

我尝试了以下解决方案(上述),但似乎在针对 Confluence 进行身份验证时遇到问题。我检查了权限,我是Confluence管理组的成员。 GET 命令似乎可以工作,但是使用 POST 时,我不断收到:

({"statusCode":403,"message":"You're not allowed to view that space, or it does not exist."})

我发现了类似的问题,但没有一个有有效的解决方案。大多数资源似乎是为 python 编写的。

是否还有其他更好的 API 解决方案?我愿意接受建议。

最佳答案

再次感谢@mansilladev。在您的帮助下,我找到了解决方案。而不是使用以下方法:

beforeSend: function (xhr){
xhr.setRequestHeader('Authorization', "Basic YWRtaW46YWRtaW4= " );
}

headers: {
"Authorization": "Basic " + btoa(username + ":" + password)
}

使用xhrFields选项以及用户名密码,似乎可以执行成功的请求。

我的解决方案

$.ajax({
url: link,
type: "POST",
dataType: "json",
crossDomain: true,
username: user.username,
password: user.password,
data: JSON.stringify(data),
contentType: "application/json",
xhrFields: {
"withCredentials": true
},
success: function (result) {
alert('Data saved!');
},
error: function (xhr, status, error) {
alert(status + ' | ' + error);
}
});

关于jquery - 如何使用 jQuery 及其 REST API 在 Confluence 中创建新页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27970685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com