admin管理员组文章数量:1122826
I'm trying to call controllers on a Rails Backend. While on my dev servers on the same domain, all works fine.
Access-Control-Allow-Origin *
is present on the Apache Server.
const res = await fetch(`${PUBLIC_ADMIN_URL}/api_users.json`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'content-type': 'application/json'
},
body: JSON.stringify({"user": user, "password": password})
})
Will work on my Dev servers on the same domain. But I have to remove content-type': 'application/json'
for it to POST to the Rails backend without a CORS error in the Browser. Now though, the payload is lost somewhere, params don't get passed on to the Rails app.
I think this is not a Rails issue, but an Apache configuration.
I'm trying to call controllers on a Rails Backend. While on my dev servers on the same domain, all works fine.
Access-Control-Allow-Origin *
is present on the Apache Server.
const res = await fetch(`${PUBLIC_ADMIN_URL}/api_users.json`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'content-type': 'application/json'
},
body: JSON.stringify({"user": user, "password": password})
})
Will work on my Dev servers on the same domain. But I have to remove content-type': 'application/json'
for it to POST to the Rails backend without a CORS error in the Browser. Now though, the payload is lost somewhere, params don't get passed on to the Rails app.
I think this is not a Rails issue, but an Apache configuration.
Share Improve this question edited Nov 21, 2024 at 14:14 Christian Meichtry asked Nov 21, 2024 at 13:46 Christian MeichtryChristian Meichtry 958 bronze badges 5 |1 Answer
Reset to default 0I had to remove 'content-type': 'application/json'
for the request to reach the Rails App, but then the JSON would not be posted.
Now I build a FormData
object instead and post that object. Problem solved :)
const loginData = new FormData();
loginData.append("user", user)
loginData.append("password", password)
const res = await window.fetch(`${PUBLIC_ADMIN_URL}/api_users.json`, {
method: 'POST',
headers: {
'Accept': 'application/json',
// 'content-type': 'application/json'
},
body: loginData
})
本文标签: SvelteKit fetch to RailsApache Backend CORS issueStack Overflow
版权声明:本文标题:SvelteKit fetch to RailsApache Backend CORS issue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736310321a1934335.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
rack-cors
for more details on how to accomplish this task. – engineersmnky Commented Nov 21, 2024 at 17:43Access-Control-Allow-Origin *
– Christian Meichtry Commented Nov 23, 2024 at 12:52