2017-07-14 9 views
0

私はwww.otherwebsite /ロガー、例えば、alert(allText);から別のサーバーにコンテンツを記録しようとしている?現在のポッピングであるalert msgなし= ALLTEXT()を記録。言い換えれば XMLHttpRequest()を使用してコンテンツをログサーバーに転送する方法は?

は、どのように私はXMLHttpRequestのを使用して allTextの情報をログサーバに別の要求を生成することができますか?

私はjsbinでスクリプトを実行していた、私は現在、コンテンツをロードするために、このスクリプトを使用していますが、私はテストのためにallText

<script> 
    function readTextFile(file) 
    { 
     var rawFile = new XMLHttpRequest(); 
     rawFile.open("GET", file, false); 
     rawFile.onreadystatechange = function() 
     { 
      if(rawFile.readyState === 4) 
      { 
       if(rawFile.status === 200 || rawFile.status == 0) 
       { 
        var allText = rawFile.responseText; 
        alert(allText); 
       } 
      } 
     } 
     rawFile.send(null); 
    } 

    readTextFile("http://null.jsbin.com/runner"); 

</script> 

と私のログサーバーに別の要求を生成するかどうかはわかりません。 com

何かアドバイスやご提案をいただければ幸いです。

答えて

1

あなたがデータを投稿したいためのAPIにネストされたポストコールしてください:

<script> 
    function readTextFile(file) 
    { 
     var rawFile = new XMLHttpRequest(); 
     rawFile.open("GET", file, false); 
     rawFile.onreadystatechange = function() 
     { 
      if(rawFile.readyState === 4) 
      { 
       if(rawFile.status === 200 || rawFile.status == 0) 
       { 
        var allText = rawFile.responseText; 
        var xhr = new XMLHttpRequest(); 
        xhr.open("POST", '/server', true); 

        //Send the proper header information along with the request 
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

        xhr.onreadystatechange = function() { 
        //Call a function when the state changes. 
         if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) { 
         // Request finished. Do processing here. 
         } 
        } 
        xhr.send(allText); 
       } 
      } 
     } 
     rawFile.send(null); 
    } 

    readTextFile("http://null.jsbin.com/runner"); 

</script> 
+0

感謝を。これは私が探していたものです。 POSTを使う方がはるかに優れています。 – pancho

関連する問題