dev-resources.site
for different kinds of informations.
Sending cookies with Cross Origin (CORS) request
Published at
2/9/2020
Categories
javascript
php
xhr
cors
Author
zubairmohsin33
Author
14 person written this
zubairmohsin33
open
Implementation:
We need to do two things:
- Include
withCredentials : true
in your Ajax request.
For plain XMLHttpRequest like below:
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://cross_origin_url', true);
xhr.withCredentials = true;
xhr.send(null);
For jQuery:
$.ajax({
url: //cross origin url
xhrFields: {
withCredentials: true
}
})
- Secondly, from your server side we need to send a Response header which is: Access-Control-Allow-Credentials and set its value to true.
Access-Control-Allow-Credentials: true
PHP example:
header('Access-Control-Allow-Credentials: true');
In Laravel we can do:
public function index()
{
return response()->header('Access-Control-Allow-Credentials', true);
}
Security Concerns:
DDoS
. If you have setAccess-Control-Allow-Origin: *
, any person with any domain will be able to send request to your URL.If someone can copy the Cookie value from browser ( even if its encrypted ) and send it along with request, it will be a legit request.
- Consider throttling ( rate limiting ) for such urls in your application.
- Perform verification in a middleware for such request to verify its coming from a trusted source.
That's it ๐๐ผ Happy Coding ๐จ๐ฝโ๐ป
xhr Article's
11 articles in total
A Comprehensive Guide with XHR, Fetch API, Axios and jQuery AJAX
read article
2่กไปฃ็ ๏ผ่งฃๅณ้ๆๅฒ็ช๏ผ่ชๅจๅธฎไฝ ๅๆถ้ๅค็่ฟๆ่ฏทๆฑ
read article
How to Target XHR Errors in Cypress
read article
Angular SSR Refused to set unsafe header
read article
What Is XHR And Why You Should Be Looking At XMLHttpRequests
read article
Making simple HTTP requests in NodeJS
read article
Ajax and XHR using plain JS
read article
Intro to Ajax & XHR
read article
OMDb-search (JS code demo)
read article
Is it possible to build an application with Webpack that uses XHR to load pages of the site? If so, which plugins do you use?
read article
Sending cookies with Cross Origin (CORS) request
currently reading
Featured ones: