Cross domain put call does not work with Access-Control-Allow-Origin

前端 未结 1 832
星月不相逢
星月不相逢 2021-01-26 00:14

I am facing problem related to cross domain PUT call , i have allowed Access-Control-Allow-Origin from server side put still it doesn\'t work.<

1条回答
  •  囚心锁ツ
    2021-01-26 00:55

    Instead of adding all the CORS headers inside your resource method, use a Jersey filter, as described in this post. The reason for this, is the CORS preflight request, which is defined in HTTP access control (CORS) as:

    "preflighted" requests first send an HTTP request by the OPTIONS method to the resource on the other domain, in order to determine whether the actual request is safe to send.

    So the request is an OPTIONS request and it expects back the the "Accept-Xxx" CORS headers to determine what is allowed by the server. So putting the headers in the resource method has no affect as the the request is made with the OPTIONS HTTP method, which you don't have a resource method for. This generally leads to a 405 Method Not Allowed error sent to the client.

    When you add the headers in the filter, every request goes through this filter, even the OPTIONS request, so the preflight gets the according headers.

    As for the PUT, also described in the above linked document (continuing from the above quote)

    Cross-site requests are preflighted like this since they may have implications to user data. In particular, a request is preflighted if:

    • It uses methods other than GET, HEAD or POST. Also, if POST is used to send request data with a Content-Type other than application/x-www-form-urlencoded, multipart/form-data, or text/plain, e.g. if the POST request sends an XML payload to the server using application/xml or text/xml, then the request is preflighted.
    • It sets custom headers in the request (e.g. the request uses a header such as X-PINGOTHER)

    This is why the POST request doesn't face the same problem.

    0 讨论(0)
提交回复
热议问题