Solved

AngularJS $http POST Error Statas Messages?

Posted on 2014-04-21
3
5,366 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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

705 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