我开始用 JavaScript 制作这个简单的 google chrome 扩展。在代码的开头我有以下内容:
var req = new XMLHttpRequest();
req.open(
"GET",
"http://www.ldoceonline.com/dictionary/manga",
true);
req.onreadystatechange(alert(req.readyState));
req.readyState 的值变为 1,这意味着所需的页面尚未正确获取。我是 JavaScript 新手。我的代码有什么问题?
最佳答案
像这样怎么样
var request = new XMLHttpRequest();
if (request == null){
alert("Unable to create request");
}else{
var url = "http://www.ldoceonline.com/dictionary/manga";
request.onreadystatechange = function()
{
if(request.readyState == 4)
{
LDResponse(request.responseText);
}
}
request.open("GET", url, true);
request.send(null);
}
function LDResponse(response)
{
// do stuff with the response
}
当然,这都是假设他们向您返回有效的数据,即 XML 或 json
关于javascript - 在 Google Chrome 扩展程序中使用 XMLHttpRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3238013/