簡體   English   中英

jQuery 從 fetch 中選擇數據值

[英]jQuery selecting data value from fetch

我遇到了 jquery 的另一個問題。 我看到了使用 .ajax() 方法的代碼,並且正在像這樣選擇響應

.then(response => {
  $(response).find('.some-selector')
})

這真的讓我很困惑。

我像這樣對香草js進行了重構

fetch('/some-endpoint', {
  method: 'POST',
  body: new FormData(e.currentTarget)
})
.then(res => res.text())
.then(data => {
  data.querySelector('.some-selector') // does not work
})

一切正常,響應良好,但我不明白如何從數據中獲取所選值。

您需要先解析響應:

 fetch('/some-endpoint', { method: 'POST', body: new FormData(e.currentTarget) }) .then(res => res.text()) .then(function(html) { const parser = new DOMParser(); return parser.parseFromString(html, "text/html"); }) .then(data => { data.querySelector('.some-selector'); })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM