2016-07-29 28 views
1

私はFetch APIに探し始め、私のサンドボックス内で、私はそれが私がからフェッチした場合XML応答をしたい、ステータスコード0を返すAPIを取得

function getGithubGists() { 
    fetch('https://api.github.com/users/octocat/gists') 
    .then(function(response) { 
     response.json().then(function(data) { 
     console.log('data', data) 
     }) 
    }) 
    .catch(function(error) { 
     console.log('Error', error) 
    }) 
} 

を経由してJSONオブジェクトの配列を返すようにhttps://api.github.com/users/octocat/gistsからフェッチするために取得することができますXMLを返すプライベートAPI、どのような変更が必要ですか?私は

headers: { 
    'Accept': 'text/xml' 
    } 

を追加しましたが、私は未定義の状態コード0とコンソールのプリントデータを取得しておきます。フェッチはレスポンスがJSONであることを前提としているためですか?

また、Chrome DevToolsの[ネットワーク]タブには、私が期待しているXML応答が表示されます。

私はにconsole.log

おかげでこのXMLレスポンスを印刷したい

<?xml version="1.0"?> 
<psgxml> 
    <years> 
    <year>1974</year> 
    <year>1952</year> 
    <year>1928</year> 
    </years> 
</psgxml> 

答えて

0

あなたは解決response.text()

function getGithubGists() { 
    fetch('https://privateapi.com/xml') 
    .then(function(response) { 
     response.text().then(function(data) { 
     console.log('data', data) 
     }) 
    }) 
    .catch(function(error) { 
     console.log('Error', error) 
    }) 
} 
+0

ありがとうございます!何が間違っているのか分かりました。私は別のデータが必要です。まず、約束がある。 – Nikkawat

0

にそれを変更、オブジェクトにXMLを解析するために失敗しているresponse.json()を呼んでいます! :D

私は「いいえ 『をアクセス制御 - 許可 - 起源』ヘッダーを読み込むことができませんAPIのフェッチ」エラーを受信して​​いたので、私は、サーバーでいくつかの設定を有効にする必要がありましたことを
function getXML() { 
    fetch('https://privateapi.com/xml')  
    .then(response => response.text()) // the promise 
    .then(data => console.log('Data', data)) // data 
    .catch(error => console.log('Error', error)) 
} 

1.Figuredアウト...

2.最初の1つは約束を処理し、次に2番目は約束事を処理し、次に応答をテキストに変換するために使用することができます。

関連する問題