How do I SET a Cookie (header) with XMLHttpRequest in JavaScript?

前端 未结 5 775
一个人的身影
一个人的身影 2020-11-30 00:43

I\'m trying to set a Cookie in a XSS request using XMLHttpRequest.

I found the XMLHttpRequest Specification, and section 4.6.2-5 does seem to suggest that setting Co

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 01:08

    This can be done. You need the following in the $.ajax call:

    xhrFields: {
        withCredentials: true
    }
    

    (See the jQuery docs), and you'll also need the site you're making the request to to support CORS (they will at least need to allow you origin and also to set the Access-Control-Allow-Credentials HTTP header to true).

    There's no question it works. You can do it over HTTPS, with Basic Auth, etc. jQuery will send everything (the auth header, cookies) if you tell it to (xhrFields) and the site provides the right CORS headers. Don't give up!

提交回复
热议问题