简体   繁体   English

无法使用 JavaScript 从 URL 中获取 JSON

[英]Can't get JSON out of URL with JavaScript

I've recently started to learn JavaScript, my learning process was going smoothly until I got to JSON, when I started trying to get a JSON out of a URL I got stuck real bad, I've tried loads of methods I found here, and loads of different URL's and nothing seems to work. I've recently started to learn JavaScript, my learning process was going smoothly until I got to JSON, when I started trying to get a JSON out of a URL I got stuck real bad, I've tried loads of methods I found here,并加载不同的 URL,但似乎没有任何效果。 Well cutting to the chase, this is what I've got:切入正题,这就是我所拥有的:

  • I am using IIS to create a local server, and I'm running my script from there;我正在使用 IIS 创建本地服务器,并从那里运行我的脚本;
  • I created a simple JSON file on JSONbin.io;我在 JSONbin.io 上创建了一个简单的 JSON 文件;
  • I'm trying to access it using the code I got from JSONbin's website;我正在尝试使用从 JSONbin 网站获得的代码来访问它;
  • I created some console.logs for debugging;我创建了一些用于调试的 console.logs;
  • I found out that although the console doesn't point out any error, the request function never runs;我发现虽然控制台没有指出任何错误,但请求 function 永远不会运行;
  • MYKEY is the secret key i got from JSONbin.io MYKEY 是我从 JSONbin.io 得到的密钥

If anyone could shed a light on this matter, or simple providing a simple code that would work for me, it would help a lot.如果有人能阐明这个问题,或者简单地提供一个对我有用的简单代码,那将有很大帮助。

 <p> test </p> <script> let req = new XMLHttpRequest(); req.onreadystatechange = () => { if (req.readyState == XMLHttpRequest.DONE) { console.log("1"); console.log(req.responseText.name); } }; req.open("POST", "http://api.jsonbin.io/b/5ec04a83a47fdd6af1645b86", true); console.log("2"); req.setRequestHeader("Content-Type", "application/json"); req.setRequestHeader("secret-key", "MYKEY"); console.log("3"); </script>

You've simply forgot to send() the request您只是忘记了send()请求

 <p> test </p> <script> let req = new XMLHttpRequest(); req.onreadystatechange = () => { if (req.readyState == XMLHttpRequest.DONE) { console.log("1"); console.log(req.responseText.name); } }; req.open("POST", "http://api.jsonbin.io/b/5ec04a83a47fdd6af1645b86", true); console.log("2"); req.setRequestHeader("Content-Type", "application/json"); req.setRequestHeader("secret-key", "MYKEY"); req.send(); // <--------------------- Check this console.log("3"); </script>

In order to execute your request you missed req.send() at the end.为了执行您的请求,您最后错过了req.send() You have to check the console and the network tab in the inspector, there you will see if you have other problems.您必须检查检查器中的控制台和网络选项卡,在那里您会看到是否有其他问题。

Be aware that in some browsers like Firefox you can be blocked with the message "Blocked loading mixed active content".请注意,在 Firefox 等某些浏览器中,您可能会被“阻止加载混合活动内容”消息阻止。 That's happening when you are in a HTTPS page and you execute your script using a HTTP url (like you did).当您在 HTTPS 页面中并使用 HTTP url 执行脚本时,就会发生这种情况(就像您所做的那样)。 That is a security action in order to avoid attacks like MITM due to posting potentially secure content (your key or other stuff).这是一种安全措施,可以避免由于发布潜在安全内容(您的密钥或其他内容)而引发的 MITM 等攻击。

Yup you simply forget send()是的,您只是忘记了 send()

const req = new XMLHttpRequest()
req.onreadystatechange = function () {
  // In local files, status is 0 upon success in Mozilla Firefox
  if(req.readyState === XMLHttpRequest.DONE) {
    var status = req.status;
    if (status === 0 || (status >= 200 && status < 400)) {
      // The request has been completed successfully
      console.log(req.responseText);
    } else {
      // Oh no! There has been an error with the request!
    }
  }
};
req.open("POST", "http://api.jsonbin.io/b/5ec04a83a47fdd6af1645b86", true);
console.log("2");
req.setRequestHeader("Content-Type", "application/json");
req.setRequestHeader("secret-key", "MYKEY");
console.log("3");
req.send(); // try this 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM