問題所在
。我正在使用Lichess API,其中在獲取用戶的匯出游戲中指出,查詢引數max是整數型別,可以設定>=1。
在下面的代碼片段中,我無法獲取 Network devtools說 有人知道這是怎么回事嗎? uj5u.com熱心網友回復: 來的資料格式是nd-json和Lichess API提供一個實用功能以幫助閱讀NDJSON流式回應。 如果你使用這個,你將獲得資料: 如果你使用這個,你將獲得資料。
標籤:max大于1的資料,但對于max=1,它是有效的。它在這個相同的JSFiddle中也不作業,在我的VsCode專案中也是如此。只有當我把查詢的max設定為1時,它們才會起作用。
。
new Vue({
el: "#app"/span>,
data: {
temp: 1.
},
methods: {
async fetchData() {
const response = await fetch (
`https://lichess.org/api/games/user/Thibault?max=2&rated=false`,
{headers:{
Accept:'application/x-ndjson'。
}}
);
const data = await response.json();
console.log(資料)。
},
}
})body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
<script src="https://cdnjs. cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>/span>
<div id="app">/span>
<button @click="fetchData()"/span>> 獲取資料</button>。
</div>我已經嘗試過的事情
我試著使用async await,并洗掉它們,但沒有成功,我試著在網上搜索解決方案,并徹底閱讀API檔案,但就是不成功。
content-type是x-ndjson,所以我在header標簽中設定了application/x-ndjson(解決了我之前的另一個第一個問題)。new Vue({
el: "#app"/span>,
data: {
temp: 1.
},
methods: {
async fetchData() {
const response = await fetch (
`https://lichess.org/api/games/user/Thibault?max=2&rated=false`,
{headers:{
Accept:'application/x-ndjson'。
}}
).then(readStream(onMessage)
.then(onComplete)。
console.log(arr)。
},
}
})
const readStream = processLine => response => /span> {
const stream = response.body.getReader()。
const matcher = /span>.
?
/。
const decoder = new TextDecoder() 。
let buf = ''/span>;
const loop = () =>
stream.read().then(({ done, value }) => {
if (doed) {
if (buf.length > 0) processLine(JSON.parse(buf))。
} else {
const chunk = decoder.decode(value, {
stream: true
});
buf = chunk;
const parts = buf.split(matcher)。
buf = parts.pop()。
for (const i of parts. filter(p => p) processLine(JSON.parse(i)) 。
return loop()。
}
});
return loop();
}
const arr = [] 。
const onMessage = obj => arr.push(obj)。
const onComplete = (/span>) => console. log('The stream has completed') 。
