RXJS按钮单击“不获取服务器响应”



我有一个节点JS服务器端演示类型应用程序。我通过遵循PluralSight的教程来做到这一点。并与 rxjs 一起使用Typescript进行反应性编程。

它工作正常,直到我尝试使用一个按钮与单击事件链接并获取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>

addEventListener的正确事件名称需要为loadonload,如下所示:https://developer.mozilla.org/en-us/docs/web/web/api/xmlhtttpprequestfreqreeqeeste/p>

最新更新