【问题标题】:.getJSON response not happening.getJSON 响应没有发生
【发布时间】: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

标签: c# jquery api


【解决方案1】:

问题是我的 API 响应签名没有被修饰为异步。即使我打电话给 .result ,它也变得一团糟。我的解决方案是修改我的代码如下

 Public Async Function ValidateBlobExists(id As String) As Task(Of JsonResult(Of Boolean))
         result = await CDNHelper.BlobExists(id, "ioc")
         return result
End function

public static async Task<bool> BlobExists()
{
return client postasync wrapper
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 2020-08-15
    • 1970-01-01
    • 2013-03-13
    相关资源
    最近更新 更多