[英]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:切入正题,这就是我所拥有的:
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.