Lance
Lance

Reputation: 491

jquery $.post error when called from Bootstrap Modal

In a web app I am developing I display a Modal dialog using Bootstrap. Within that dialog I have form elements and a save button. When the user clicks save I fire a function which calls $.post to invoke an asp.net mvc action to save the data entered.

The browser (Chrome) displays the following message when I click the Save button:

"Something Unexpected Happened :(....." .

After confirming the error message the asp.net action is invoked and the rest of the script completes fine. I also see the same error if I use $.ajax.

IE gives slightly different behaviour. Isee the same message in IE but it appears after the success callback function completes.

If I call the save function from outside the modal dialog I do not see this error message.

function saveUser() {
    var userId = $('#userId').val();
    var fullName = $('#fullName').val();
    var userName = $('#userName').val();
    var isAdmin =  $('#isAdmin').is(':checked');
    var authenticationMode =  $('#userAuthenticationMode').val();

    $.post(
        "@Url.Action("Save", "Account")",
        {
            userId :  userId,
            fullName : fullName,
            userName : userName,
            isAdmin : isAdmin,
            authenticationMode : authenticationMode
        }, 
        function (data) {
            if (data.Success) {
                alert("success");
            }
            else {
                alert("failed");
            }
        }
    );

Here is the modal declaration:

<div id="update_user_modal" class="modal hide">
      . 
      .
      .    
    <div  style="float:right">
        <button id="SaveUser" class="btn btn-primary">Save changes</button>
    </div>
</div>

Update:

Found the source of the alert message. There's an ajax error handler in the code which a colleague failed to mention:

// Set up AJAX error handling ...
$(document).ajaxError(function (event, jqXHR, ajaxSettings, thrownError) {
if (jqXHR.status == 404) {
    $("#NotFoundInfoDialog").dialog("open");
} else if (jqXHR.status == 500) {
    $("#InternalServerErrorDialog").dialog("open");
} else {
    alert("Something unexpected happend :( ...");
  }
});

Any ideas on how I can debug the ajax error. jqXHR.status returns 0.

Update When I use $.ajax to make an ajax call the following occurs:
First time I attempt to save an unknown ajax exception is raised and the modal closes.
If I display the modal again and attempt to Save it works fine.
If I know try to display the modal again and save I get an 'Internal Server error'. My mvc action is working fine.

Update Have no idea why but removing the form tags in the modal div solved the problem.

Thanks,

Lance

Upvotes: 2

Views: 2239

Answers (2)

Lance
Lance

Reputation: 491

There were tags inside my modal. This is what was causing the problem. No idea why. I'll post the problem on the twitter bootstrap users list.

Upvotes: 0

Dau
Dau

Reputation: 8858

you have bind ajaxError in wrong way, if status is not 404 or 500 then it always go to else block which display the error "something unexpected happend", so change it

$(document).ajaxError(function (event, jqXHR, ajaxSettings, thrownError) {
if (jqXHR.status != 200){
 if (jqXHR.status == 404) {
     $("#NotFoundInfoDialog").dialog("open");
 } else if (jqXHR.status == 500) {
     $("#InternalServerErrorDialog").dialog("open");
 } else{
     alert("Something unexpected happend :( ...");
   }
 }
});

Upvotes: 1

Related Questions