无法从 wiki api 接收数据

Posted

技术标签:

【中文标题】无法从 wiki api 接收数据【英文标题】:Can't receive data from 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');
    
);

【问题讨论】:

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

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

要么;

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

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

马特

【讨论】:

以上是关于无法从 wiki api 接收数据的主要内容,如果未能解决你的问题,请参考以下文章

Android 无法通过 API 接收多条记录

cURL 错误 28 即使 API 端工作,主机不接收数据?

无法使用 Spring 使用 java REST Api 接收 json 数据

无法定期在 IOS 中进行后台 API 调用以推送应用程序中从 BLE 设备接收的数据

使用 REST API 从 Web 服务接收 JSON 数据

从 API 接收数据并将其分配给模型的问题