我为20+年写了钱的代码,但我没有理由问这个问题。
天哪,我在谷歌上查过了。我知道这里有许多半相同的问题,我想我已经阅读了所有的回答,并尝试了所有的模式建议。许多问题并没有得到很好的阐述,我的观点似乎有点自相矛盾(所有的部分都是孤立的)。请容忍我,我会尽力的。
前部使用svelte-试剂盒,背面使用fastapi。
首先,下面是我的"Buttons.svelte“组件,它是逐字复制的。
<script lang="javascript">
async function worksJustFine() {
fetch("https://jsonplaceholder.typicode.com/todos")
.then(response => {
console.log(" response", response)
console.log(" r.json() >", response.clone().json())
response.json()
.then(json => {
console.log("json", json)
})
.catch(error => console.log(error))
})
}
async function doesNotWork() {
fetch(`http://localhost:8000/api/test_url?test_param=1`)
.then(response => {
console.log(" response", response)
console.log(" r.json() >", response.clone().json())
response.json()
.then(json => {
console.log("json", json)
})
.catch(error => console.log(error))
})
}
</script>
<buttons>
<button class="btn btn-outline-primary btn-lg" on:click={worksJustFine}>OK</button>
<button class="btn btn-outline-primary btn-lg" on:click={doesNotWork}>NOT OK</button>
</buttons>
<style lang="scss">
</style>第二,以下是路线/视图:
@router.get("/api/test_url")
async def test_url(test_param: str):
try:
print(f"test_param: {test_param}")
return {"success": True, "message": "Message", "hooray": True}
except Exception as e:
print(repr(e))
return {"success": False, "message": repr(e), "": True}问题表现如下:
app = FastAPI()
app.add_middleware(CORSMiddleware,
allow_origins=["http://localhost:3000"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"])
app.include_router(router)worksJustFine工作正常,它似乎不是一个svelte/javascript问题。test_url工作良好(作为POST或GET)来自除fetch API以外的每个源,所以它似乎不是fastAPI问题。我遗漏了什么?我还能提供什么更多的信息?
发布于 2022-06-25 16:10:43
感谢您抽出时间在这里阅读和评论。我已经克服了这个问题,尽管我并没有被清除掉残存的混乱。
我理解错误的来源,我把SSE指定为“一看到就用websockets代替的垃圾”。
再次感谢你抽出时间和我谈谈。
“我希望我能拥抱你们.但我不会”
https://stackoverflow.com/questions/72722315
复制相似问题