Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    On error always calls success to jQuery from an MVC HttpPost

    This is my javascript code

    var options = {
    iframe: true,
    type: 'post',
    url: "api/Registration/Register",
    data: { "data": jsonData },
    dataType: 'json',
    beforeSend: function () {
    $('#result').html('Loading response...');
    },
    beforeSubmit: function (formData, formObject, formOptions) {


    },
    uploadProgress: function (event, position, total, percentComplete) {
    },
    success: function (responseText, statusText, xhr, $form) {

    if (statusText == 'success') {

    }
    else { //code == 'FAIL'

    }
    }
    })
    },

    error: function (response, status, err) {

    },
    };
    $('#registration').ajaxSubmit(options);



    and my controller looks like this


    [HttpPost]
    public Task<HttpResponseMessage> Register()
    {

    var task = Request.Content.ReadAsMultipartAsync(provider).
    ContinueWith<HttpResponseMessage>(t =>
    {

    var response = new HttpResponseMessage
    {
    StatusCode = HttpStatusCode.OK,
    Content = new StringContent(JsonConvert.SerializeObject(assistedRegResponse.Response))
    };
    return response;
    return Request.CreateResponse(HttpStatusCode.OK);
    }
    catch (Exception ex)
    {
    throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
    }
    });

    return task;
    }


    but whenever there is an error or Exception it always goes to success callback. what i am missing here?

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Indent your code for legibility and easy debugging.

    When I indented your code using jsbeautifier.org, this is what I got.

    PHP Code:
    var options = {
        
    iframetrue,
        
    type'post',
        
    url"api/Registration/Register",
        
    data: {
            
    "data"jsonData
        
    },
        
    dataType'json',
        
    beforeSend: function () {
            $(
    '#result').html('Loading response...');
        },
        
    beforeSubmit: function (formDataformObjectformOptions) {


        },
        
    uploadProgress: function (eventpositiontotalpercentComplete) {},
        
    success: function (responseTextstatusTextxhr$form) {

            if (
    statusText == 'success') {

            } else { 
    //code == 'FAIL'

            
    }
        }
    })
    },

        
    error: function (responsestatuserr) {

        },
        };
    $(
    '#registration').ajaxSubmit(options); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •