Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is my javascript code and my controller looks like this
but whenever there is an error or Exception it always goes to success callback What am i missing here

JavaScript
var options = { 
    iframe: true,
    type: 'post',
    url: "api/Registration/Register",
    data: { "data": jsonData },
    dataType: 'json',
    success: function (responseText, statusText, xhr, $form) 
    {                 
        if (statusText == 'success') { 
        }
        else { //code == 'FAIL'
        }
    },
    error: function (response, status, err) {
    }};

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


C#
[HttpPost]
public Task<HttpResponseMessage> Register()
{
    //create task object and read server data
    try
    {
        var response = new HttpResponseMessage
        {
            StatusCode = HttpStatusCode.OK,
            Content = new StringContent(JsonConvert.SerializeObject(assistedRegResponse.Response))
        };
        return response;
     }
     catch (Exception ex){ throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));}           
     return task;
}
Posted
Updated 17-Nov-13 23:37pm
v3
Comments
♥…ЯҠ…♥ 15-Nov-13 1:06am    
Put a break point in try block,debug and see what you get in response?
Dipali Kolhe 15-Nov-13 1:14am    
When there an exception it goes to catch and throws an exception. but in java-script it executes success callback even though there is an error/exception on server side

1 solution

Hi,


If it is going to Success function means the server side code is executing successfully(Round trip is completed with no errors). If there are any syntax errors those can be catched in the error function.

In your case if it is executing the success method, it means everything is fine.
 
Share this answer
 
Comments
Dipali Kolhe 18-Nov-13 17:04pm    
but what should happen when there is no endpoint on server side... It goes to catch block on server side.. but calls ajax success

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900