3

使用以下 jquery 调用时,我收到 http 405 method not allowed 错误消息。有谁知道如何解决这个问题?是因为https吗?我也尝试使用 GET 和 PUT 类型,结果相同

  function make_base_auth(user, password) {
        var tok = user + ':' + password;
        var hash = btoa(tok);
        return "Basic " + hash;
    }   
function createJiraIssue() {

        var datos = {
            "fields": {
                "project":
              {
                  "key": "HELP"
             },
                "summary": "Test Ticket",
                "description": "Creating of an issue using project keys and issue type  names using the REST API",
                "issuetype": {
                    "name": "Bug"
                },
                "assignee": { "name": "sim" }
            }
        };

        var parameters = JSON.stringify(datos);
        var req = $.ajax({
            url: 'https://xxx.jira.com/rest/api/2/issue/',
            type: "POST",
            data: parameters,
            contentType: 'application/jsonp',
            dataType: 'jsonp',
            async: false,
            processData: false,
            beforeSend: function (xhr) {
            xhr.setRequestHeader('Authorization', make_base_auth(user, password));
            },
            error: function (errmsg) {
                alert('error ocured:' + errmsg.responseText);
            },
            success: function (text) {
                alert(text);
            },

        });
    }

编辑它必须是 contentType: 'application/json', dataType: 'json' 并且 405 错误消失。

但现在代码最终出现在错误回调中。我将其中的警报功能更改为 alert('error ocured:' + errmsg.error); 它给了我这个:错误发生:

function (){
if(!e){
  var c=arguments,g,h,i,j,k;b&&(k=b,b=0);
  for(g=0,h=c.length;
        g<h;g++)i=c[g],j=d.type(i),j==="array"?f.done.apply(f,i):j==="function"&&a.push(i);
        k&&f.resolveWith(k[0],k[1])
 }return this
}

在 chrome 中,我在控制台中收到错误: XMLHttpRequest cannot load https://xxx.jira.com/rest/api/2/issue/。Access-Control-Allow- Originhttp://localhost:49592不允许 Origin。有人知道错误是什么吗?

4

3 回答 3

2

I'm not sure if it's possible. The error you got is saying that you can't do cross domain XMLHttp Request. you can find more information on this answer.

One way to go around it, is to create a local API, using php (or any other language), that will serve the ajax calls made by jQuery.

For example, write a PHP page that will create issues via the REST API, then use the javascript to post to this page all the needed details.

Let me know if you need any help.

EDIT

to do it using C#, The easiest way ill probably be using something like JiraRestClient.NET.

If you prefer to code it yourself, here is some info about connecting to Jira using REST.

于 2012-12-18T09:05:11.693 回答
1

Your content type should be 'application/json', not 'application/jsonp'.

于 2012-12-14T19:17:19.767 回答
0

I had the same issue and was due to the fact I was using HTTP instead of HTTPS.

于 2021-06-30T15:38:50.453 回答