我正在尝试使用“Amplifyjs”来处理 AJAX 请求,就像 John Papa 在他的 Pluralsight 类(class)中所做的那样,但我在身份验证方面遇到了问题。
我正在使用表单例份验证。一切正常。
我的问题来自未经身份验证的请求。我找不到让“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
}
});
谢谢。
最佳答案
如果您想要 XHR 对象并传递它,您可以创建一个解码器。它将包含错误代码和您可能需要的其他信息。
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/ 来测试上面的代码
关于javascript - Amplifyjs 和状态码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13073685/