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
  • @jonrsharpe please explain your edits? Are there any rules I broke? – Christian Meichtry Commented Nov 21, 2024 at 14:25
  • 1 Add your CORS configuration and the error message to the question – dbugger Commented Nov 21, 2024 at 14:42
  • Have you configured CORS in the rails application stack? See rack-cors for more details on how to accomplish this task. – engineersmnky Commented Nov 21, 2024 at 17:43
  • @engineersmnky The request did not reach the Rails app and was blocked by Apache. – Christian Meichtry Commented Nov 23, 2024 at 12:51
  • @dbugger Access-Control-Allow-Origin * – Christian Meichtry Commented Nov 23, 2024 at 12:52
Add a comment  | 

1 Answer 1

Reset to default 0

I 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