【问题标题】:Jquery Json requests adds a dot at the end of the requestJquery Json requests 在请求末尾添加一个点
【发布时间】:2019-04-14 09:52:22
【问题描述】:

我正在开发一个从 API 请求一些数据的网站。 我请求了很多数据,所以我使用了一个模板字符串。

模板字符串被正确解析,但在 json 中却搞砸了。 它随机为一些数字加了一个“.”。在数字的末尾。

例如"http://logs.tf/json/2223521" => "http://logs.tf/json/2223521."

需要注意的是,我使用的是 async: false

这是我的代码:

    function myTest() {
        for (Id =2223535; Id >= 2223500; Id--) {
            console.log(Id, `http://logs.tf/json/${Id}`);//returns the proper value
            $.getJSON(`http://logs.tf/json/${Id}`, function (idData) { //sometimes adds a dot to the url
                console.log("success");
            });
        }
    }

例如我从控制台得到的(对于 myTest()):

2223523 "http://logs.tf/json/2223523"
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://logs.tf/json/2223523. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).  (unknown)
2223522 "http://logs.tf/json/2223522"
success
2223521 "http://logs.tf/json/2223521"
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://logs.tf/json/2223521. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

【问题讨论】:

  • 很好奇。几乎肯定是您的代码中的错误。您可以修改 myTest() 添加与 myTestfix() 相同的 getjson 调用并在此处显示结果吗?
  • @VanquiishedWombat 实际上我刚刚意识到,.replace(".","") 并不能解决问题,因为它还替换了主 URL (logs.tf) 中的点。我的错误
  • 这是“访问控制允许来源”。请看stackoverflow.com/questions/31276220/…

标签: javascript jquery json getjson template-strings


【解决方案1】:

好吧,我想我想通了。 它与我发出 Api 请求的 api 服务有关,如果我执行的请求太多,我会从上面得到错误。

如果我添加一些任意计算,例如“let number = 452^2”,问题就会消失,因为它会延迟一个请求和下一个请求之间的时间。

它也没有神秘地添加一个“。”对于刚刚出现的请求,因为吐出的错误在句末使用了一个点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    相关资源
    最近更新 更多