3

I'm trying to use "Amplifyjs" to handle AJAX requests as does John Papa in his Pluralsight course but I'm having problems with authentication.

I am using form authentication. Everything works fine.

My problem comes with the unauthenticated requests. I can not find a way to make "amplifyjs" give back to the error function the http code (401, 403 ...) to distinguish requests that failed because they were not authenticated from requests that failed because did not met the business logic.

A request example would be:

amplify.request.define("products", "ajax", {
                url: "/api/Products",
                datatype: "json",
                type: "GET"
            });
amplify.request({
                    resourceId: "products",
                    success: callbacks.success,
                    error: function (datos, status) {
                              //somecode
                           }
                });

Thank you.

Julián Yuste
  • 1,472
  • 10
  • 22

2 Answers2

6

You can create a decoder if you want the XHR object and pass that along. It will have the error code and other information you may need.

amplify.request.define("products", "ajax", {
    url: "http://httpstat.us/401",
    datatype: "json",
    type: "GET",
    decoder: function ( data, status, xhr, success, error ) {
        if ( status === "success" ) {
            success( data, xhr );
        } else if ( status === "fail" || status === "error" ) {
            error( status, xhr );
        } else {
            error( status, xhr );
        }
    }
});

amplify.request({
    resourceId: "products",
    success: function(data, status) {
        console.log(data, status);        
    },
    error: function(status, xhr) {
        console.log(status, xhr);
    }
});​

You can test the above code by looking at this http://jsfiddle.net/fWkhM/

Elijah Manor
  • 17,923
  • 17
  • 72
  • 79
  • Sorry, i'll post a comment as a new answer. I can't format the text right in the comment ( or I don't know how ) – Julián Yuste Nov 07 '12 at 18:19
  • @Elijah Manor, can you supply a working solution on how to get the xhr value back to the success callback? I created a custom decoder but the success callback always seems to get overridden by the default. thanks – stvn Sep 16 '15 at 15:04
  • That and if you put it on enough calls, it just seems to break which takes away the use that it should give. I'm honestly baffled as to why we only get a default 'success' or 'error'. – Nickvda Oct 31 '16 at 13:38
2

Thanks for your answer.

Finally, as I saw no one answered me I did something similar to what you propose:

var decoder = function (data, status, xhr, success, error) {
    if (status === "success") {
        success(data, status);
    } else if (status === "fail" || status === "error") {
        try {
            if (xhr.status === 401) {
                status = "NotAuthorized";
            }
            error(JSON.parse(xhr.responseText), status);
        } catch (er) {
            error(xhr.responseText, status);
        }
    }
};

After I modify the default decoder:

amplify.request.decoders._default = decoders.HeladeriaDecoder;

And in the error callback I managed the returned status.

error: function (response, status) {
    if (status === "NotAuthorized") {
        logger.error(config.toasts.errorNotAuthenticated);
    } else {
        logger.error(config.toasts.errorSavingData);
    }
//more code...
}
Julián Yuste
  • 1,472
  • 10
  • 22