2

When I want to sent a POST request to my server it works, but the parameters are empty. In another topic on stackoverflow, I read that it could be a problem with the headers. Also also tried it with different browsers and sometimes it works. These are the headers I set on the serverside:

headers['Access-Control-Allow-Origin'] = 'http://localhost:8080'
headers['X-Frame-Options'] = 'http://localhost:8080'
headers['Access-Control-Allow-Credentials']= 'true'
headers['Access-Control-Allow-Headers']= 'origin, content-type, content-length, accept, accept-language, accept-encoding'

In the attached Screemshot you can see the client headers

When I try to add headers on the client side the request also always turns into a options-request. Please help!! Thanks for all answers in advance!

This is the screenshot of the options-request:!

ada91
  • 828
  • 2
  • 7
  • 19

1 Answers1

2

The OPTIONS request is the pre-flight request that is used to confirm that the server will accept the actual request - this is part of CORS.

Once the OPTIONS request gets a response (and assuming the response says "go ahead") then the actual POST will be sent. The data should be sent along with this POST - not with the OPTIONS request.

Fenton
  • 241,084
  • 71
  • 387
  • 401
  • Thanks for that answer! The problem then is, that the options request is not accepted by the server. I modified my client and made another screenshot.. you will find it in my original question. – ada91 Mar 19 '13 at 14:48
  • The server has to respond to the OPTIONS request in order to confirm it supports a cross-origin request. This is part of the security mechanism in CORS - both client and server must confirm participation. – Fenton Mar 19 '13 at 16:26
  • what does he has to answer?.. now I do it like this [link]( http://stackoverflow.com/questions/4351904/sinatra-options-http-verb) .. everything seems okay, but the client doesn't send a post request after the options request. No error etc... – ada91 Mar 19 '13 at 18:04
  • Update: actual situation: client sends options-request, server replies, client sends post-request. BUT again empty parameters on the server side. :-( Why? I tried to do it exactly like this http://www.html5rocks.com/en/tutorials/cors/?redirect_from_locale=de#toc-handling-a-not-so-simple-request but my headers are slightly different.. – ada91 Mar 19 '13 at 19:22
  • also the data is sent with the post request, not with the options! – ada91 Mar 19 '13 at 21:11
  • Yes - I might have mentioned that in the answer. `The data should be sent along with this POST - not with the OPTIONS request.` – Fenton Mar 20 '13 at 08:43
  • Yes, I just added this information so that you know, that this is not the problem. It doesn't still work unfortunately. – ada91 Mar 20 '13 at 14:07
  • I solved the problem now.. it is really weird. When you use IE, the params hash of sinatra includes the post params also,.. but for firefox, chrome, safari it is only in request.body.read! Also I forgot content-type in access-control-allow-headers. :-) – ada91 Mar 20 '13 at 17:47