我有两个请求,一个是ajax
,另一个是fetch
,它是继任者。`fetch`请求不显示想要的结果,`ajax`请求确实有
如果我将我的数据与fetch
一起发布到与ajax
完全相同的(blackbox)API,结果会有所不同。
阿贾克斯
$.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;
});
结果:
我的主要问题是,我需要jwt
变量Ajax
返回,但在Fetch
实现中,我没有在响应中获得该变量。
我希望你们其中一个能向我解释我能做些什么来改变Fetch
的实现,或者读取我想可能在ReadableByteStream
中的变量,但我不确定。
'response.text()。then(t => console.log(t))'output? – robertklep
谢谢@robertklep,它是HTML。看起来'Ajax'只是忽略了HTML,而'Fetch'没有告诉我内容类型。这是一个服务器端问题。我打算给这个团队打电话来解决:) – Randy
你确定这不是一个认证失败吗? '$ .post'调用发送格式为'username = hello&password = world'的主体,而我假设'body:json(...)'引起'fetch'调用发送格式为'{“username的主体“:”你好,“密码”:“世界”}'我假设(除非你有非常强大的服务器端代码)服务器设置为只读取其中的一种格式 – apsillers