简体   繁体   English

当我将代码转到对象Javascript AJAX时,类型为undefined

[英]undefined type when i turned my code to Object Javascript AJAX

i don't understand why i get TypeError (this.req is undefined) on line : if (this.req.readyState === 4) { 我不明白为什么我在行上收到TypeError(this.req是未定义的):if(this.req.readyState === 4){

function RequestCORS(url) {
this.url = "http://crossorigin.me/" + url;
this.req = new XMLHttpRequest();
}

RequestCORS.prototype.send = function () {
this.req.open("GET", this.url);
this.req.onreadystatechange = function() {
    if (this.req.readyState === 4) {
        if (this.req.status === 200) {
            console.log(this.req.responseText);
        } else {
            console.log("error request");
            //handleError
        }
    }
};
this.req.send();
};

function main() {
var url = "http://www.01net.com/rss/mediaplayer/replay/";
var requete = new RequestCORS(url);

requete.send();
}


window.addEventListener("load", main);

Thanks for reading. 谢谢阅读。

this.req is undefined because you're making an asynchronous request and by the time your onreadystatechange fires this doesn't refer to your RequestCORS instance anymore. this.req是未定义的,因为您正在发出异步请求,并且在您的onreadystatechange触发时, this不再引用您的RequestCORS实例。

You could declare a local variable that remains in scope inside the onreadystatechange function. 您可以声明一个保留在onreadystatechange函数内部范围内的局部变量。

var req = this.req;
this.req.onreadystatechange = function() {
  if (req.readyState === 4) {
    if (req.status === 200) {
        console.log(req.responseText);
    } else {
        console.log("error request");
        //handleError
    }
  }
};

or use bind 或使用绑定

this.req.onreadystatechange = function() {
  if (this.req.readyState === 4) {
    if (this.req.status === 200) {
        console.log(this.req.responseText);
    } else {
        console.log("error request");
        //handleError
    }
  }
}.bind(this);

or get rid of this.req entirely 或完全摆脱this.req

var req = new XMLHttpRequest();
req.onreadystatechange = function() {
  if (req.readyState === 4) {
    if (req.status === 200) {
        console.log(req.responseText);
    } else {
        console.log("error request");
        //handleError
    }
  }
};

暂无
暂无

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

相关问题 我可以通过javascript代码打开Firefox跟踪保护吗? - Can I know when Firefox Tracking Protection is turned on in javascript code? 当我在 Javascript 中返回 Object 时未定义 - Undefined when i am returning an Object in Javascript 调用 URL 时对 JavaScript 代码感到困惑,不确定是否需要 Ajax 调用 - Confused on my JavaScript code when calling a URL and not sure if I need an Ajax call 如何从我的 Ruby on Rails API 解析这些哈希转换为字符串,以便它可以作为 object 在 Z9E1713B69D1D24ADA9 中访问 - How can I parse these hashes turned string, from my Ruby on Rails API so that it's accessible as an object in Javascript? 这是我使用 javascript 和 Z2705A83A5A0659CCE34583972637EDA 发送 JSON object 的代码 - here is my code to send JSON object using javascript and ajax 将一个PHP对象从ajax文件返回到我的javascript代码 - returning a PHP object from an ajax file to my javascript code 当我在表中插入日期 object 时,JSON ajax 返回 undefined - JSON ajax returns undefined when I insert a date object in the table 当我的console.log类型的函数返回一个对象时,它的类型为undefined。 我不明白为什么 - When I console.log type of my function which returns an object it gives undefined. I don't understand why 我在我的代码中使用纯 javascript 得到未定义的错误 - I get undefined error using pure javascript in my code 为什么我的JavaScript对象中的属性未定义? - Why am I getting property undefined in my JavaScript object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM