Javascript RXjs按钮单击不获取服务器响应

Javascript RXjs按钮单击不获取服务器响应,javascript,json,node.js,get,rxjs,Javascript,Json,Node.js,Get,Rxjs,我有一个NodeJS服务器端演示类型的应用程序。我通过pluralsight完成了一个教程。使用Typescript和rxjs进行反应式编程 它工作得很好,直到我尝试了一个按钮来链接一个点击事件,并得到一个json对象来显示在客户端。我将发布代码。请有人告诉我为什么它不工作 main.ts import {Observable} from "rxjs"; let output=document.getElementById('output'); let button=document.getE

我有一个NodeJS服务器端演示类型的应用程序。我通过pluralsight完成了一个教程。使用Typescript和rxjs进行反应式编程

它工作得很好,直到我尝试了一个按钮来链接一个点击事件,并得到一个json对象来显示在客户端。我将发布代码。请有人告诉我为什么它不工作

main.ts

import {Observable} from "rxjs";

let output=document.getElementById('output');
let button=document.getElementById('button');
let click=Observable.fromEvent(button,"click");


function LoadMovies(url:string){

let xhr=new XMLHttpRequest();
xhr.addEventListener("LoadMovies",()=>{
    console.log("yeah");//this is for debugging, and I noticed that this line is never execute so the below lines to put movies to the client site doesnt work either.
    let movies=JSON.parse(xhr.responseText);
    movies.forEach(m => {
        let div=document.createElement("div");
        div.innerText=m.title;

        output.appendChild(div);
    });
})
xhr.open("GET",url);
xhr.send();
}


click.subscribe(
    e=> LoadMovies("movies.json"),
    e => console.log(`error: ${e}`),
    () => console.log("complete")   
);
movies.json

[
{
    "title": "Star Wars"
},
{
    "title": "Star Trek"
},
{
    "title": "Starship Troopers"
}
]
index.html

<html>

<head>
   <title>RxJs</title>
   <style>  
       #circle {
           width: 20px;
           height: 20px;
           border-radius: 50%;
           background-color: red;
           position: absolute;
       }
   </style>
</head>
<body>

    <button id="button">Get Movies</button>
    <div id="output"></div>
    <script src="app.js"></script>// this is actually the entry point of the app which links to main.ts code and it works just fine. 
</body>
</html>

RxJs
#圈{
宽度:20px;
高度:20px;
边界半径:50%;
背景色:红色;
位置:绝对位置;
}
拍电影
//这实际上是链接到main.ts代码的应用程序的入口点,它工作正常。

此处指定的
addEventListener
的正确事件名称需要是
load
onload

这是什么
xhr.addEventListener(“LoadMovies”…)
XMLHttpRequest
类肯定不会发出任何名为
“LoadMovies”
的事件。也许您想使用
onload
来代替。教程使用了名为“load”的函数来实现此目的,并且相同的名称“load”也被传递给了xhr.addEventListener,因此我认为因为我的函数使用了名称“LoadMovies”,所以我也将其传递给了eventlistener。请参阅并查找“事件处理程序”标题。我通过将方法更改为“加载”解决了此问题。非常感谢:)请将其添加为答案,以便我可以将其标记为答案:)