Search code examples
javascripttrello

Trello API isnt calling the success handler?


I have an issue where we have the Trello API adding a new card to a defined list, and it is successfully adding to the list, however we are trying to use the .done to close the current window and open a hard-coded trello board. This code is used to take a record from CRM and copy the code to Trello.

    var creatingtheCard = function () {
        var record = getRecord(getParameterByName("id"))

        if (record.Description == null) {
            record.Description = "";
        }

        var options = document.getElementById("ListNameItem");
        var listId = options[options.selectedIndex].value

        Trello.post("cards", { name: record.Title, desc: record.Description + " - " + record.CustomerId.Name, idList: listId})
         .done(closeWinOpenTrello)
         .error(failure);
    }

    function closeWinOpenTrello() {
        window.open("http://trello.com/b/8QWBDiTI")
        window.close()
    }

This function is called and it successfully creates the new card in Trello, but it wont perform the closeWinOpenTrello function, but it will perform the .error.

Also i ran this in the debugger and when i run the code step by step it will give the error and then close the window and open a new window with Trello.

Thanks in advance

Update 1

This is the failure function

    var failure = function (error) {
        alert(error.statusText);
    }

Solution

  • You need to cancel the form submission. Otherwise the page changing will cause the cancelled state of the request - the browser isn't going to bother waiting for the response after the page changes, because there's no longer anything waiting on the response.

    In order to do this, just return false from the onsubmit handler.