Solved

AngularJS $http POST Error Statas Messages?

Posted on 2014-04-21
3
5,297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
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-…
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…

739 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