Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

NetworkError on JIRA REST call because of no preemptive request

Lukasz Gornicki February 18, 2014

Hi,

I created a confluence plugin that should render an InlineDialog (part of AUI) with a form. Basing on the input in the form, a REST call to JIRA is performed to create an issue.

this is what I have in my js file. Part that reacts on Send button from the form:

AJS.$("#send").click(function() {
                            var login = $("#email1").val();
                            var password = $("#password1").val();
                            var summary = $("#summary").val();
                            var details = $("#details").val();
                            var address = $("#address").val();
                            var str = login + ':' + password;
                            var credentials = utf8_to_b64(str);
                            function utf8_to_b64( str ) {
  return window.btoa(unescape(encodeURIComponent( str )));
}
                            AJS.$.ajax({
                                beforeSend: function (xhr) {xhr.setRequestHeader("Authorization", "Basic " + credentials);},
                                url: "https://myjira.com/rest/api/2/issue",
                                type: "POST",
                                dataType: "json",
                                contentType: "application/json; charset=utf-8",
                                data: formToJSON(summary, details),
                                async: false,
                                success: function (data) {
                                                alert(data);
                                },
                                error: function (xhr, ajaxOptions, thrownError) {
                                      alert(xhr.status);
                                      alert(thrownError);
                                      alert(xhr.responseText);
                                      alert(xhr);
                                    },
                                
                            });
                            function formToJSON(summary, details) {
                                return JSON.stringify({"fields":{"project":{"key":"DOC"},"summary":summary,"description":details,"components":[{"name":"SureThing"}],"issuetype":{"name":"Bug"},"assignee":{"name":"meMyselfAndI"}}});
                                                  }
                            
                                    
                            }
                        )

- so values from the form are taken right, I tested with with alert()

- encoding for credentials is done right, as I did the decoding and I got what I wanted to get

- json data is good as I used it in some random rest client and it worked

The problem is with what I think some initial empty request or something. I noticed that when I test the rest request with a rest client it works only if I tick option "preemptive" next to the username and password. So I guess I need something like this in my ajax ca. I hoped my beforeSend would do the needful, but it somehow didn't.

Reponse status code i 0 and the error: NetworkError: A network error occured

Any ideas?

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Lukasz Gornicki February 26, 2014

So the solution was a bit complicated but is at least effective :) I had to proxy the request, so:

1. I've created a resource class that on POST is delegating data to an object with a method that with jersey ussage is making a proper POST call to jira that works and is not blocked

2. My ajax script is not calling this internal confluence resource class

1 vote
Lukasz Gornicki February 20, 2014

I came acros even bigger problem. What I described above was tested on my local machine. So after reading as much as possible about the issue, I figured out that maybe it will work fine once I deploy the plugin on confluence test machine that is in the same domain as my jira production instance. So I did it. My tests were triggered on newest confluence distribution. I deployed plugin on conf 5.1.5 and it doesn't work....I mean the code snippet I pasted above is not invoked like it was in higher version.

I should be invoked after send button click from here:

AJS.InlineDialog(AJS.$("#popupLink"), 1,
            function(content, trigger, showPopup) {
                content.css({"padding":"40px"}).html('<form method="POST" class="aui"><h3>Your Jira Credentials</h3><fieldset><div class="field-group"><label for="email1">Username<span class="aui-icon icon-required"> required</span></label><input class="text" type="text" id="email1" name="email" title="email" ></div><div class="field-group"><label for="password1" accesskey="p">Password<span class="aui-icon icon-required"> required</span></label><input class="password" type="password" id="password1" name="password" title="password"></div><hr><h3>Issue Description</h3><div class="field-group"><label for="summary">Issue Title<span class="aui-icon icon-required"> required</span></label><input class="text long-field" type="text" id="summary" name="summary")"></div><div class="field-group"><label for="details">Issue Details<span class="aui-icon icon-required"> required</span></label><textarea class="textarea" rows="6" cols="10" name="details" id="details"></textarea></div><div class="field-group"><label for="address">Page Address<span class="aui-icon icon-required"> required</span></label><input class="text long-field" type="text" id="address" name="address" disabled></div></fieldset><fieldset><div class="buttons-container"><div class="buttons"><input class="button submit" type="button" value="Send" id="send"></div></div></fieldset></form>');
                document.getElementById("email1").value=email;
                document.getElementById("address").value=pageUrl;
                showPopup();
                AJS.$("#send").click(function() {
                and the rest...

Can anybody tell me why 2 different conf version (5.4.2 and 5.1.5) react differently on the same js?

Lukasz Gornicki February 26, 2014

I've solved this issue with different behavior on 5.1.5 by adding the following to the velocity file

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

A bit surprising as I thought jQuery is bundled with Confluence. Nevertheless it works now and only this matters

TAGS
AUG Leaders

Atlassian Community Events