【发布时间】:2018-12-09 01:47:53
【问题描述】:
有人可以换一双眼睛吗?我正在尝试在显示之前验证 blob 图像是否存在。这可能需要 1-4 秒。 我的 JS 看起来像这样:
var url = '/api/blob/ValidateBlobExists?id=' + blobImage;
$.getJSON(url,
function(json) {
console.log("success");
})
.done(function () {
console.log("second success");
exists = data;
if (exists) {
console.log("exists'");
$('#imgPhotograph').hide().attr('src', blobImage).fadeIn();
} else {
$('#imgPhotograph').attr('src', '../Images/NoPhotoFound.jpg');
}
});
api 看起来像这样.. 请不要判断.. 它的 vb 因为它必须是。
Public Function ValidateBlobExists(id As String) As JsonResult(Of Boolean)
dim result = CDNHelper.BlobExists(id) 'this could take ~5 seconds
Return Json(Of Boolean)(result)
End Function
底层方法如下所示:
public static bool BlobExists(string filename)
{
try
{
var sw = new Stopwatch();
sw.Start();
do
{
if (client.AssertBlobExists(filename).Result) // <-- this is a wrapper to query the azure blob
return true;
System.Threading.Thread.Sleep(500); //no reason to hammer the service
} while (sw.ElapsedMilliseconds < 8000);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
return false;
}
问题在我的控制台输出中,我什至无法获得“成功”。似乎 getJson() 只是不愿意等待 8 秒再继续。任何想法表示赞赏。
【问题讨论】:
-
添加
fail()回调以查看请求是否因某种原因失败。检查开发工具的网络选项卡以查看请求是否实际通过 -
^^^ 并验证请求上的响应实际上是有效的json