DEBUGGING BOOKS 2

So I slacked off a little on my reading Pro AngularJS and also detoured a bit into reading up on Bootstrap since I am relatively inexperienced with it and the book more or less assumes you know it (or will at least ignore it 🙂 ).

I started reading through Chapter 7 and encountered some issues with the following code:

angular.module("sportsStore")
    .constant("dataUrl", "http://localhost:5500/products")
    .controller("sportsStoreCtrl", function ($scope, $http, dataUrl) {

        $scope.data = {};

        $http.get(dataUrl)
            .success(function (data) {
                $scope.data.products = data;
            })
            .error(function (error) {
                $scope.data.error = error;
            });
    });

In the section “Handling Ajax Errors” the author states “The object passed to the error function defines status and message properties.”.  Actually this is not the case, the error function should have the signature

.error(function (data, status, headers, config) {

Also, the first parameter is not an error object, it is the response data. For example in the case of a 404 error it would be the returned HTML 404 page.

The following code should make the example work


            .error(function (data, status, headers, config) {
                $scope.data.error = {};
                $scope.data.error.data = data;
                $scope.data.error.status = status;
            });

Secondly to test the error handling and achieve a 404 error, the author states in a tip to change the URL to something like ‘http://localhost:5500/doesNotExist’. This will most likely not work , as the site is running under nodejs on port 5000, and Deployd is hosting under port 5500, the browser will see this as a CORS request and most likely not return any error information, similar to the angular issue described in the link below. If you want to get a 404 error change the URL to a non-existent relative URL something like ‘/doesNotExist’, this will be relative to the site URL and be a non-CORS request.

CORS Error Response Status = 0

https://github.com/angular/angular.js/issues/2798

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

OR

LOGIN OR REGISTER

Registered users with one approved comment can comment without moderation