我对xhr返回事件感到困惑,正如我所知,onreadystatechange - > readyState == 4和onload 之间没有太多不同,是真的吗?
var xhr = new XMLHttpRequest();
xhr.open("Get", url, false);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4)
{
/* do some thing*/
}
};
xhr.send(null);
Run Code Online (Sandbox Code Playgroud)
要么
xhr.onload = function() { /* do something */ }
Run Code Online (Sandbox Code Playgroud) 这是我的代码:
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();
xhr.open("GET", "//URL")
xhr.setRequestHeader("Content-Type: application/json", "Authorization: Basic //AuthKey");
xhr.send();
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Cannot find module 'xmlhttprequest'
Run Code Online (Sandbox Code Playgroud)
当我删除第一行时,我得到:
XMLHttpRequest is not defined
Run Code Online (Sandbox Code Playgroud)
我已经搜遍过各地,人们已经提到Node.js的问题,但我的Node安装是正确的,所以我不确定是什么问题.
我正在查看以下API:
http://wiki.github.com/soundcloud/api/oembed-api
他们给出的例子是
呼叫:
http://soundcloud.com/oembed?url=http%3A//soundcloud.com/forss/flickermood&format=json
Run Code Online (Sandbox Code Playgroud)
响应:
{
"html":"<object height=\"81\" ... ",
"user":"Forss",
"permalink":"http:\/\/soundcloud.com\/forss\/flickermood",
"title":"Flickermood",
"type":"rich",
"provider_url":"http:\/\/soundcloud.com",
"description":"From the Soulhack album...",
"version":1.0,
"user_permalink_url":"http:\/\/soundcloud.com\/forss",
"height":81,
"provider_name":"Soundcloud",
"width":0
}
Run Code Online (Sandbox Code Playgroud)
我需要做什么才能从一个网址获取这个JSON对象?