[英]jQuery REST PUT request doesn't work in my code?
我只想在Jira中使用jQuery提出PUT請求。 我曾經在SoapUI上進行過嘗試,並且可以正常工作,但是在我的JS文件中卻無法正常工作……它總是給我返回錯誤(在我的情況下,警告“否”)。
這是我的代碼:
var issueKey = this.JIRA.Issue.getIssueKey();
var username = "admin";
var password = "admin";
var encodedLoginData = btoa(username + ":" + password);
AJS.$.ajax({
type: 'PUT',
contentType: 'application/json',
url: '/jira/rest/api/2/issue/' + issueKey,
dataType: 'json',
async: false,
headers: { 'Authorization': 'Basic ' + encodedLoginData },
data: JSON.stringify('{"update":{"timetracking":[{"edit":{"originalEstimate":"4m","remainingEstimate":"3m"}}]}}'),
success: function(response){ alert("yes"); },
error: function(error){ alert("no"); }
});
如前所述,JSON數據短語在SoapUI中有效,登錄信息和base64加密也適用。 沒錯。 但是我找不到我的錯...有什么想法嗎?
編輯:
PUT http://localhost:2990/jira/rest/api/2/issue/TEST-3 400
XMLHttpRequest.send @ batch.js?devtoolbar=…logged-in=true:5461
send @ batch.js?locale=en-US:197
ajax @ batch.js?locale=en-US:191
calculate @ batch.js?devtoolbar=…logged-in=true:5620
prepareCalculation @ batch.js?devtoolbar=…logged-in=true:5620
(anonymous) @ batch.js?devtoolbar=…logged-in=true:5620
dispatch @ batch.js?locale=en-US:104
h @ batch.js?locale=en-US:96
trigger @ batch.js?locale=en-US:101
simulate @ batch.js?locale=en-US:108
e @ batch.js?locale=en-US:114
如果這是IIS服務器,則可能需要禁用WebDAV,因為它會捕獲所有PUT請求。
我認為您的問題是JSON.stringify的參數不應為String。 嘗試將其保存到變量中,然后對其進行JSON.stringify。
考慮到JSON.stringify的結果。 例如:
JSON.stringify("{}"); //""{}""
JSON.stringify({}); //"{}"
現在您的代碼應如下所示:
var issueKey = this.JIRA.Issue.getIssueKey();
var username = "admin";
var password = "admin";
var encodedLoginData = btoa(username + ":" + password);
var dataObject = {"update":{"timetracking":[{"edit":{"originalEstimate":"4m","remainingEstimate":"3m"}}]}};
AJS.$.ajax({
type: 'PUT',
contentType: 'application/json',
url: '/jira/rest/api/2/issue/' + issueKey,
dataType: 'json',
async: false,
headers: { 'Authorization': 'Basic ' + encodedLoginData },
data: JSON.stringify(dataObject),
success: function(response){ alert("yes"); },
error: function(error){ alert("no"); }
});
發生您的錯誤是您正在嘗試對字符串進行字符串化
data: JSON.stringify('{update...}')
如今,您不需要jQuery在瀏覽器中執行HTTP。 所有現代瀏覽器均內置有Fetch API
const issueKey = this.JIRA.Issue.getIssueKey();
const username = "admin";
const password = "admin";
const encodedLoginData = btoa(username + ":" + password);
const body = {
update: {
timetracking: [{
edit: {
originalEstimate: "4m"
remainingEstimate: "3m"
}
}]
}
}
fetch(`/jira/rest/api/2/issue/${issueKey}`, {
method: 'PUT',
body: JSON.stringify(body),
headers: {
'Authorization': 'Basic ' + encodedLoginData
'Content-Type': 'application/json',
},
})
.then(response => alert('yes'))
.catch(error => alert('no'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.