简体   繁体   中英

Angular JS $http POST not responding on success in ASP.NET MVC

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:

在此处输入图片说明

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

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!');
   });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM