Solved

AngularJS $http POST Error Statas Messages?

Posted on 2014-04-21
3
5,230 Views
Last Modified: 2014-05-01
I found a very nice AngularJS Client/Server example here...

My question concerns the $http error callback partially works. A simple Alert box displays 'status' as number and 'error' as [object, Object]...

Q. How do I return the actual error message?

[Client (AngularJS) - Register User]
  function registerUser(userData) {

            var accountUrl = serverBaseUrl + "/api/Account/Register";

            var deferred = $q.defer();
            $http({
                    method: 'POST',
                    url: accountUrl,
                    data: userData,
                }).success(function (data, status, headers, config)
                {
                    console.log(data);
                    deferred.resolve(data);

                }).error(function (error, status)
                {
                    alert(error);      // [object, object]
                    alert(status);    // 400
                    console.log(error);
                    deferred.reject(status);
                });

            return deferred.promise;
        }

[Server (Web Api 2) - Register User] 
[AllowAnonymous]
        [Route("Register")]
        public async Task<IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            IdentityUser user = new IdentityUser
            {
                UserName = model.UserName
            };

            IdentityResult result = await UserManager.CreateAsync(user, model.Password);
            IHttpActionResult errorResult = GetErrorResult(result);

            if (errorResult != null)
            {
                return errorResult;
            }

            return Ok();
        }

Open in new window

0
Comment
Question by:WorknHardr
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 400 total points
ID: 40013429
I suspect--based on what I see in the documentation--that you need to add a few parameters to your function definition:

...
.error(function (data, status, headers, config, statusText)  // renamed "error" to "data" based on documentation
                {
                    alert(statusText);
                    alert(status);    // 400
                    console.log(error);
                    deferred.reject(status);
                });

Open in new window

0
 

Author Comment

by:WorknHardr
ID: 40013571
Now it returns this:
 }).error(function (data, status, headers, config, statusText)
                {
                    alert(data);        // [object, Object]
                    alert(status);      // 400
                    alert(headers);     // Text...
                    alert(config);      // [object, Object]
                    alert(statusText);  // undefined
0
 

Author Closing Comment

by:WorknHardr
ID: 40034801
thx
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question