在 Google Chrome 扩展程序中使用 XMLHttpRequest [英] Using XMLHttpRequest in a Google Chrome extension

查看:212
本文介绍了在 Google Chrome 扩展程序中使用 XMLHttpRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始用 javascript 制作这个简单的 google chrome 扩展.在代码的开头,我有以下内容:

I started making this simple google chrome extension in javascript. And in the beginning of the code I have the following:

var req = new XMLHttpRequest();

req.open(
    "GET",
    "http://www.ldoceonline.com/dictionary/manga",
    true);

req.onreadystatechange(alert(req.readyState));

值 req.readyState 变为 1,这意味着尚未正确获取所需的页面.我是 Javascript 的新手.我的代码有什么问题?

The value req.readyState comes to be 1, which means the required page has not been properly fetched. I'm a newbie to Javascript. What's the problem in my code?

推荐答案

这样的事情怎么样

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

Of course this is all assuming that they are giving you valid data back ie XML or json

这篇关于在 Google Chrome 扩展程序中使用 XMLHttpRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆