gpt4 book ai didi

javascript - `fetch` 请求未显示所需结果,`ajax` 请求显示所需结果

转载 作者:行者123 更新时间:2023-11-30 15:58:45 25 4
gpt4 key购买 nike

我有两个请求,一个使用 ajax,另一个使用 fetch,后继者。

如果我使用 fetch 将我的数据发布到与 ajax 完全相同的(黑盒)API,结果是不同的。

Ajax

$.post(this.config.baseUrl + this.config.loginUrl, {
username:this.username,
password:this.password
})
.done(data => {debugger;});

// result: {"jwt":"eyJ0eX..."}

获取

this.http.fetch(this.config.baseUrl + this.config.loginUrl, {
method: 'post',
body: json({
username:this.username,
password: this.password
})
})
.then(response => {
// let x = response.json();
// If I uncomment the above, I get an error:
// undefined:1 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
debugger;
});

结果:

enter image description here

我的主要问题是我需要 Ajax 返回的 jwt 变量,但是在 Fetch 实现中我没有得到那个变量在响应中。

我希望你们中的任何一个能向我解释我可以做些什么来改变 Fetch 实现,或者读取我猜可能在 ReadableByteStream 中的变量>,但我不确定。

最佳答案

Fetch 返回一个具有某些属性的响应,body 就是您需要的那个。获取时主体的数据类型是 ReadableByteStream

在你的情况下,最简单的事情就是将它转换为 json,这非常简单:

this.http.fetch(this.config.baseUrl + this.config.loginUrl, {
method: 'post',
body: json({
username:this.username,
password: this.password
})
})
.then(response => {response.json().jwt});

关于javascript - `fetch` 请求未显示所需结果,`ajax` 请求显示所需结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38077839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com