Los*_*les 4 javascript es6-promise fetch-api asp.net-core asp.net-core-webapi
使用javascript提取并调用rest服务,如果成功,则该服务返回blob,否则返回错误消息json。如何在提取中进行处理?实际的服务是asp.net Web API实现,成功时将返回FileStreamResult(或FileContentResult),否则返回带有json且包含错误消息的错误代码。以下是我要执行的操作的示例:
fetch('flowers.jpg').then(function(response) {
if(response.ok) {
return response.blob();
} else {
return response.json();
}
}).then(function(myBlob) { // here I would also like to function(jsonError)
var objectURL = URL.createObjectURL(myBlob);
myImage.src = objectURL;
}).catch(function(error) {
console.log('There has been a problem with your fetch operation: ', error.message);
});
Run Code Online (Sandbox Code Playgroud)
由于您想沿着两条截然不同的路径前进,因此这是一种相对罕见的情况,您可能想嵌套处理程序:
fetch('flowers.jpg').then(function(response) {
if (response.ok) {
return response.blob().then(function(myBlob) {
var objectURL = URL.createObjectURL(myBlob);
myImage.src = objectURL;
});
} else {
return response.json().then(function(jsonError) {
// ...
});
}
}).catch(function(error) {
console.log('There has been a problem with your fetch operation: ', error.message);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
950 次 |
| 最近记录: |