javascript - Amplifyjs and status code - Stack Overflow

I'm trying to use "Amplifyjs" to handle AJAX requests as does John Papa in his Pluralsig

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 es 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.

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 es 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.

Share Improve this question asked Oct 25, 2012 at 17:06 Julián YusteJulián Yuste 1,47210 silver badges22 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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/fWkhM/

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...
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745351427a4623857.html

相关推荐

  • javascript - Amplifyjs and status code - Stack Overflow

    I'm trying to use "Amplifyjs" to handle AJAX requests as does John Papa in his Pluralsig

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信