Hi,
I’m working on a JIRA Cloud addon and I’m having some issues with the workflow of:
- Submit form in a Dialog via ajax
- Flash a message based on result
Currently, the form submits, the ajax call gets made successfully, and the addon server responds with JSON and a 200 response code.
However, the ajax .fail is triggered with a status of 0 and statusText of “error”.
Code:
app.js:
AJS.toInit(function () {
AP.require(['dialog', 'messages'], function (dialog, messages) {
AJS.$("#dialog-show-button").click(function () {
dialog.create({
key: 'create-item-dialog-key',
size: 'large',
chrome: false
});
});
// Hides the dialog
AJS.$("#dialog-close-button").click(function (e) {
e.preventDefault();
dialog.close()
});
AJS.$('#create-item-form').on('aui-valid-submit', function (e) {
var itemData = {
name: AJS.$("#item-name").val(),
description: AJS.$("#item-description").val()
};
var baseUrl = getBaseUrl();
var returnToken = getToken();
$.ajax({
beforeSend: function (request) {
request.setRequestHeader("Authorization", 'JWT ' + returnToken);
},
dataType: 'json',
contentType: "application/json; charset=utf-8",
url: baseUrl + '/item',
type: "POST",
data: JSON.stringify(itemData)
}).done(function (data) {
messages.success('Success!', 'item added');
})
.fail(function (data, statusText, three) {
messages.error('Error', 'There was a problem creating a item.')
});
e.preventDefault();
dialog.close()
});
});
});
form html in dialog:
<form id="create-item-form" class="aui">
<!-- Main dialog content -->
<div class="aui-dialog2-content">
<div class="field-group">
<label for="item-name">item name<span class="aui-icon icon-required">required</span></label>
<input class="text" type="text" id="item-name" name="item-name" title="item Name"
data-aui-validation-field
required
maxlength="1000">
<div class="description"></div>
</div>
<div class="field-group">
<label for="item-description">Description</label>
<textarea class="textarea" name="item-description" id="item-description"></textarea>
</div>
</div>
<!-- Dialog footer -->
<footer class="aui-dialog2-footer">
<!-- Actions to render on the right of the footer -->
<div class="aui-dialog2-footer-actions">
<button id="dialog-submit-button" class="aui-button aui-button-primary">Create</button>
<button id="dialog-close-button" class="aui-button aui-button-link">Cancel</button>
</div>
<!-- Hint text is rendered on the left of the footer -->
<div class="aui-dialog2-footer-hint">this is a hint</div>
</footer>
</form>
Possibly related, the message (in this case, always the Error message) will show up in Safari but not Chrome.
Chrome gives me this in the console:
[Simple-XDM] Failed to validate origin: (my ngrok url)
Any advice is greatly appreciated. Thank you!