简体   繁体   中英

jQuery REST PUT request doesn't work in my code?

I just want to make a PUT request with jQuery in Jira. I've tried it before with SoapUI and there it works, but in my JS file it's not working... It's always giving me an error back (alert with "no" in my case).

Here's my code:

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"); }
});

As mentioned, the JSON data phrase works in SoapUI, also the login information and the base64 encryption. That's all correct. But I can't find my fault... Any ideas?

EDIT:

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请求。

I think your problem is that the parameter of your JSON.stringify shouldn't be a String. Try to save that into a variable and then make a JSON.stringify of that.

Take into account the result of JSON.stringify. For instance:

 JSON.stringify("{}"); //""{}""

 JSON.stringify({}); //"{}"

Now your code should be like this For example:

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"); }
});

Happens to be your error is that you're trying to stringify a string

data: JSON.stringify('{update...}')

Nowadays, you don't need jQuery to do HTTP in the browser. All modern browsers come with the Fetch API built in

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'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM