【问题标题】:Can't receive data from wiki api无法从 wiki api 接收数据
【发布时间】:2018-10-11 22:50:56
【问题描述】:

我的代码在 codepen 中,它的链接是: https://codepen.io/mlestina/pen/OZpOQW?editors=1111

我永远无法从 Wiki API 收到任何数据。

Contr-Shift-J 读数为:

Loading failed for the <script> with source “https://en.wikipedia.org/w/api.php?action=opensearch&search=dsfds&callback=jQuery331010723807837372779_1525209741598&_=1525209741599”.  index.html:1
An iframe which has both allow-scripts and allow-same-origin for its sandbox attribute can remove its sandboxing. OZpOQW
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://static.codepen.io/assets/telefon/bold/af889c53-1ee3-4868-8fdc-2b310d587b50-3-b7a87e0fbd213943fae0c0ef5985635dd43fa9c24876b2725127a13ccaf4ab6a.woff. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). (unknown)
downloadable font: download failed (font-family: "Telefon" style:normal weight:normal stretch:normal src index:0): bad URI or cross-site access not allowed source: https://static.codepen.io/assets/telefon/bold/af889c53-1ee3-4868-8fdc-2b310d587b50-3-b7a87e0fbd213943fae0c0ef5985635dd43fa9c24876b2725127a13ccaf4ab6a.woff unknown:1:24452
Loading failed for the <script> with source “https://en.wikipedia.org/w/api.php?action=opensearch&search=Hitler&callback=jQuery33109582690097080016_1525209907566&_=1525209907567”.  index.html:1
This site appears to use a scroll-linked positioning effect. This may not work well with asynchronous panning; see https://developer.mozilla.org/docs/Mozilla/Performance/ScrollLinkedEffects for further details and to join the discussion on related tools and features!  OZpOQW

HTML 是:

<body>
<form>
  <input type="text" name="a" id="a"><br>

  <button onclick="passToJ()">Search</button>

</form>
</body>

而 Javascript 是:

function passToJ() {
    var searchTerm = document.getElementById('a').value;
    console.log("inside function, searchTerm is: ", searchTerm, " and x is: ", x);
    var wikiLink = "https://en.wikipedia.org/w/api.php?action=opensearch&search=" + searchTerm + "&callback=?";

    $.ajax({
        url: wikiLink,      
        dataType: "jsonp",
        type: "GET",
        success: function(data) {
            console.log(data); 
        },
        error: function() {
        console.log('error occurred');
    }
})};

【问题讨论】:

  • 你读过那个错误信息了吗?我认为信息很清楚
  • 格式和拼写
  • 如果很清楚,我就不会问这个问题了。我仍然对此一无所知。

标签: javascript ajax api


【解决方案1】:

我终于从另一个论坛得到了答案。

要么;

1) 包括 event.preventDefault();在 javascript 代码中,或

2) 消除 HTML 中的 Form 和 /Form 标签

马特

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多