Solved

AngularJS $http POST Error Statas Messages?

Posted on 2014-04-21
3
4,873 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 74

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now