0

I am developing an ASP.NET MVC Application. I am using Angular JS. Now what I am doing is posting data to server and getting JSON result from the server. But my problem now is, it sends data to server, but responding data to client or success callback of $http is not working. But error callback working.

My server action method

public JsonResult Login(LoginViewModel model)
        {
            FormError formError = new FormError();
            if (ModelState.IsValid)
            {




//I do nothing here yet
        }

        formError.Status = false;
        List<string> errors = new List<string>();
        foreach (ModelState modelState in ViewData.ModelState.Values)
        {
            foreach (ModelError error in modelState.Errors)
            {
                errors.Add(error.ErrorMessage);
            }
        }
        formError.ErrorMessages = errors;
        // If we got this far, something failed, redisplay form
        return Json(formError);
    }

I send like this in angular

 $http.post('Login', { UserName: $scope.email, Password: $scope.password }, function (response) {
                 alert('success')
            }).error(function (error) {
                alert('Opps! Something went wrong!');
            })

In the Angular code, if internal error occurred, error callback called. Besides, the data are correctly passed to the post method and can be logged at the server.

As you can see here action successfully return data:

enter image description here

But at the client, angular js $http success callback is not working. What is wrong with my code?

1 Answer 1

1

You haven't added a success callback to the $http.post method. The signature of $http.post is post(url, data, config?) and you seems to be trying to use it like $http.post(url, data, callback). Change it to:

$http.post('Login', { UserName: $scope.email, 
                      Password: $scope.password }).then(
   function (response) {
     alert('success')
   }, function (error) {
     alert('Opps! Something went wrong!');
   });
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. My mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.