我正在尝试使用"Amplifyjs“来处理AJAX请求,就像John Papa在他的Pluralsight课程中所做的那样,但我在身份验证方面遇到了问题。
我使用的是表单身份验证。一切都很好。
我的问题来自于未经身份验证的请求。我找不到一种方法让"amplifyjs“将http代码(401,403 ...)返回给错误函数。区分因未通过身份验证而失败的请求和因未满足业务逻辑而失败的请求。
请求示例如下:
amplify.request.define("products", "ajax", {
url: "/api/Products",
datatype: "json",
type: "GET"
});
amplify.request({
resourceId: "products",
success: callbacks.success,
error: function (datos, status) {
//somecode
}
});谢谢。
发布于 2012-11-07 14:16:09
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/来测试上面的代码
发布于 2012-11-08 02:30:58
谢谢你的回答。
最后,由于我看到没有人回答我,我做了与您的建议类似的事情:
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);
}
}
};修改默认解码器后:
amplify.request.decoders._default = decoders.HeladeriaDecoder;在错误回调中,我管理返回的状态。
error: function (response, status) {
if (status === "NotAuthorized") {
logger.error(config.toasts.errorNotAuthenticated);
} else {
logger.error(config.toasts.errorSavingData);
}
//more code...
}https://stackoverflow.com/questions/13073685
复制相似问题