3

When using the following jquery call I receive a http 405 method not allowed error message. Does anybody know how to solve this? Is it due to the https? I tried it also with type GET and PUT with the same result

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

        });
    }

EDIT It has to be, contentType: 'application/json', dataType: 'json' and the 405 error disapears.

But now the code ends up in the error callback. I changed the alert function in there to alert('error ocured:' + errmsg.error); and it gave me this: error ocured:

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
}

In chrome I get the error in the console: XMLHttpRequest cannot load https://xxx.jira.com/rest/api/2/issue/. Origin http://localhost:49592 is not allowed by Access-Control-Allow-Origin. Does anybody know what the error is?

Simon
  • 454
  • 7
  • 18

3 Answers3

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.

Community
  • 1
  • 1
Kuf
  • 17,318
  • 6
  • 67
  • 91
1

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

Perception
  • 79,279
  • 19
  • 185
  • 195
  • Yes you were right, it has to be, contentType: 'application/json', and even dataType: 'json', But now I get the error callback: error ocured: (without a responseText) ? – Simon Dec 14 '12 at 19:54
  • i changed to alert('error ocured:' + errmsg.error); and it gave me this: function (){if(!e){var c=arguments,g,h,i,j,k;b&&(k=b,b=0);for(g=0,h=c.length;g – Simon Dec 14 '12 at 20:28
  • 1
    That is ... very hard to read. Please amend your question to include the new information. – Perception Dec 14 '12 at 20:32
0

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

Tiresia
  • 91
  • 8