gpt4 book ai didi

javascript - 单击 RXjs 按钮未得到服务器响应

转载 作者:行者123 更新时间:2023-12-03 04:30:15 26 4
gpt4 key购买 nike

我有一个 Node js 服务器端演示类型应用程序。我是按照pluralsight 的教程来制作的。并使用 Typescript 和 rxjs 进行响应式编程。

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

ma​​in.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/API/XMLHttpRequest

关于javascript - 单击 RXjs 按钮未得到服务器响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43523900/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com