Solved

AngularJS $http POST Error Statas Messages?

Posted on 2014-04-21
3
5,153 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

860 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