我有一个提供JSON数据的Django后端。当我跑步时,我得到:curl 127.0.0.1:8000/posts/
[
{
"title": "This is a title",
"body": "Body :)",
"pub_date":"2020-11-25T13:36:57Z"
},
...
]
但是,当我运行此js代码时
const API = '127.0.0.1:8000/posts/'
fetch(API).then(response => console.log(response))
我得到:
Response {
type: "basic",
url: "http://localhost:3000/127.0.0.1:8000/posts/",
redirected: false,
status: 200,
ok: true,
statusText: "OK",
headers: Headers,
body: ReadableStream,
bodyUsed: false
}
这是意料之中的。如果我然后尝试运行,我会得到.then(response => response.json())
Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
当我跑步时
fetch(API).then(response => console.log(response.headers))
fetch(API).then(response => console.log(response.text()))
我得到
Headers { }
Promise { "pending "}
<state>: "pending"
分别
此外
fetch(API).then(response => console.log(response.text()))
fetch(API).then(response => response.json()).then(data => console.log(data))
只是发回
Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
错误
更新:
我还注意到在 Django 服务器日志上,当我刷新 javascript 页面时没有出现新的请求。但是,当我运行 curl 时,有一个 GET 请求。
慕运维8079593
慕村9548890
交互式爱情
相关分类