首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Amplifyjs和状态代码

Amplifyjs和状态代码
EN

Stack Overflow用户
提问于 2012-10-26 01:06:26
回答 2查看 1.9K关注 0票数 3

我正在尝试使用"Amplifyjs“来处理AJAX请求,就像John Papa在他的Pluralsight课程中所做的那样,但我在身份验证方面遇到了问题。

我使用的是表单身份验证。一切都很好。

我的问题来自于未经身份验证的请求。我找不到一种方法让"amplifyjs“将http代码(401,403 ...)返回给错误函数。区分因未通过身份验证而失败的请求和因未满足业务逻辑而失败的请求。

请求示例如下:

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

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-07 14:16:09

代码语言:javascript
复制
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);
    }
});​

您可以通过查看以下http://jsfiddle.net/fWkhM/来测试上面的代码

票数 6
EN

Stack Overflow用户

发布于 2012-11-08 02:30:58

谢谢你的回答。

最后,由于我看到没有人回答我,我做了与您的建议类似的事情:

代码语言:javascript
复制
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);
        }
    }
};

修改默认解码器后:

代码语言:javascript
复制
amplify.request.decoders._default = decoders.HeladeriaDecoder;

在错误回调中,我管理返回的状态。

代码语言:javascript
复制
error: function (response, status) {
    if (status === "NotAuthorized") {
        logger.error(config.toasts.errorNotAuthenticated);
    } else {
        logger.error(config.toasts.errorSavingData);
    }
//more code...
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13073685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档