2017-04-20 9 views
0

私はノードjsサーバサイドのデモタイプのアプリケーションを持っています。私はpluralsightを介してチュートリアルに従うことでそれを作っています。反応プログラミングのためにTypescriptとrxjsを使用しています。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> 
+0

「xhr.addEventListener( "LoadMovies" ...)」とは何ですか? 'XMLHttpRequest'クラスは、' 'LoadMovies ''と呼ばれるイベントを必ず送出しません。代わりに 'onload'を使用したかったでしょう。 – martin

+0

この目的のために "load"という名前のチュートリアル関数が使用されていて、同じ名前の "load"もxhr.addEventListenerに渡されたので、私は "LoadMovies"という名前を使用したので、よく – touseef

+0

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequestを参照して、*「イベントハンドラ」*タイトルを探します。 – martin

答えて

関連する問題