2017-07-19 4 views
0

私はangle-cliで作業しています。私はXMLHttpRequestをclickイベントを使って送信しています。リクエストはプロトコルスキームでのみサポートされています:http、data、chrome、chrome-extension、https

XMLHttpRequest cannot load file:///E:/xampp/Angular- 
]cli/Login/src/app/employees.txt. Cross origin requests are only supported for 
protocol schemes: http, data, chrome, chrome-extension, https. 

これは問題の原因です。 (file.component.ts

 rawFile.send(null);

あなたはより多くのファイルが必要な場合は、私に教えてください。ここで

は、私はあなたが関数呼び出しで file:///を指定しなければならないと思います

file.component.ts

import { Component, OnInit } from '@angular/core'; 

@Component(
{ 
selector: "file", 
templateUrl: "/filedata.component.html", 
}) 
export class FileData 
{ 
makeRequest(file) 
{ 
    let rawFile = new XMLHttpRequest(); 
    rawFile.open("GET", file, false); 
    try 
    { 
     rawFile.onreadystatechange =() => 
     { 
      if(rawFile.readyState === 4) 
      { 
       if(rawFile.status === 200 || rawFile.status == 0) 
       { 
        var allText = rawFile.responseText; 
        console.log(allText); // Here is the contents of the file 
       } 
       else 
        console.log("error contents"); 
      } 
      else 
       console.log("error ready status"); 
     } 
     rawFile.send(null); // Here's the Error! 
    } 
    catch(e) 
    { 
     console.log(e); 
    } 
} 
} 

file.component.html

<div> 
<button type = "button" (click) = "makeRequest('E:/xampp/Angular-cli/Login/src/app/employees.txt')">File Test</button> 
</div> 

答えて

0

ファイルです

<button type = "button" (click) = "makeRequest('file:///E:/xampp/Angular-cli/Login/src/app/employees.txt')">File Test</button>

+0

何もない。 :/。実行しないでください ** if(rawFile.readyState === 4)** –

関連する問題