Is is possible to make a cross domain POST ajax request of application/json?

后端 未结 2 644
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-06 19:01

I am testing some csrf stuff, and I am wondering if it is possible to POST a cross domain ajax request with Content-Type: application/json

Every time I try

2条回答
  •  说谎
    说谎 (楼主)
    2021-02-06 19:30

    The Content-Type: application/json header is not a simple header, and therefore first requires a preflight request before the actual request. The HTTP OPTIONS request you are seeing is the preflight request. From the CORS spec (http://www.w3.org/TR/cors/):

    A header is said to be a simple header if the header field name is an ASCII case-insensitive match for Accept, Accept-Language, or Content-Language, or if it is an ASCII case-insensitive match for Content-Type and the header field value media type (excluding parameters) is an ASCII case-insensitive match for application/x-www-form-urlencoded, multipart/form-data, or text/plain.

    In order to get past the preflight request, the server needs to respond to the OPTIONS request with the following headers:

    Access-Control-Allow-Origin: *
    Access-Control-Allow-Methods: GET,PUT,POST,DELETE
    Access-Control-Allow-Headers: Content-Type
    

    Once the browser receives this response, it will make the actual HTTP POST request. Note that if your request contains additional custom headers, you will need to include them in the Access-Control-Allow-Headers response header. You can learn more about CORS preflight requests here:

    http://www.html5rocks.com/en/tutorials/cors/#toc-adding-cors-support-to-the-server

提交回复
热议问题