Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JIRA Rest API error. Unrecognized token creating a issue

No luck in adding a issue via AJAX and the REST API. I can get it to work with Postmen, unfortunatly, can't get it with an Ajax request.

The JSON I create is fine, the post request also. The issuetype is something I created myself, using Bug gives the same problem. See the JSON object created, my error and my code: JSON object (this is a snippet from console.log):

snippet from the console. (I do console.log(jira)

The Error

0: "Unrecognized token 'fils5poet5': was expecting 'null', 'true', 'false' or NaN↵ at [Source: org.apache.catalina.connector.CoyoteInputStream@7b958ed2; line: 1, column: 21]"

jira = {
   fields : {
      project : {
         key : "CIC"
      },
      summary : "test",
      description: "test",
      issuetype : {
         name : "Sandbox item"
      }
   }
};

console.log(jira); //Also see image at top of this post.

// Submit to Jira api
$.ajax({
   type : "POST",
   dataType : "JSON",
   url : configuration.api_url,
   beforeSend:  function (xhr) {
      xhr.setRequestHeader ("Authorization", "Basic ItsAWrap!(itworks...)"),
      xhr.setRequestHeader ("Content-Type", "application/json");
   },
   data : jira,
   success : (function(response) {
//Do something
}})
like image 799
Chilion Avatar asked Jan 07 '15 09:01

Chilion


2 Answers

You need to JSON.stringify your jira variable before you send it.

like image 137
CarsonF Avatar answered Oct 24 '22 01:10

CarsonF


You could try something like this:

jira = {
    "fields":
    {
        "project":
        {
            "key": "CIC"
        },
        "summary": data["story.name"],
        "description": data["story.notes"],
        "issuetype": { "name": "Sandbox item" }
    }
};

//THIS BADASS FUNCTION!!!
jira = JSON.stringify(jira);

$.ajax({
    type : "POST",
    url : configuration.api_url,
    dataType : "JSON",
    async : false,
    headers: {
        "Authorization": "Basic YeahSomethingInAWrap",
        "Content-Type": "application/json",
        "Accept": "application/json",
        "Cache-Control": "no-cache"
    },
    data : jira,
    success : (function(response) {
        // Hide loader
        l.removeClass("show");

        // Alert Success Message
        alert("Melding succesvol ontvangen, bedankt!");

        // Close dialog
        $(".chrome-extension-dialog a.close").trigger("click");

    })
}); 
like image 3
Bopp Avatar answered Oct 24 '22 03:10

Bopp