'this.response' 未在控制台中打印

'this.response' is not printing in the console

    function callOtherDomain() {

        var invocation = new XMLHttpRequest();
        var username = "usr";
        var password = "pass";
        var url = 'https://someurl';  
        invocation.open('GET', url, true, username,password);  
        console.log(this.responseText);                                
        invocation.send();
    }

我无法获得适用于上述代码的响应

首先,responseText 是您从 XMLHttpRequest 获得的响应,因此它自然应该是 invocation 的成员而不是 this

其次,由于是请求的响应,在收到实际响应之前不会有任何内容。因此,如果请求是同步的,那么您可以在 send 调用之后执行。不幸的是,您将其作为异步请求来执行,这意味着您必须创建一个响应处理程序(使用 onreadystatechange 属性)并在完全收到响应后打印响应。

我建议您遵循 onreadystatechange 属性 上的 link,因为它包含一个非常简单但很好的示例,说明如何处理异步请求,完全按照您的意愿进行。