How to include the Access-Control-Allow-Origin header?

0

I'm in a small project with php and I get a video where the source is an IP camera and I need to take a picture of a video frame and convert it to base64 to save to the database, I can not use the video tag to receive the video from the IP camera, because the browser complains about the absence of the Access-Control-Allow-Origin header, how to write this header in html? I already tried it and nothing ...

    
asked by anonymous 02.06.2016 / 14:07

3 answers

0

In this case you can not do it.

The header should not go on your page, but on what is trying to be accessed remotely, in case it would have to be added on the IP camera, which is probably impossible.

In these cases, what is usually done to get around the problem is to make the server-side request, it is usually created as a "proxy", where your page uses javascript to make the request to your own site, and then you do the request by PHP for example to external resource, in case the camera, however as in this case it is a video I do not know how that would behave.

    
02.06.2016 / 15:03
-1

At some point I had this same problem, but the Access-Control-Allow-Origin header has to be added to the server side.

Add to your server:

 header('Access-Control-Allow-Origin: http://site1.com');
 header('Access-Control-Allow-Origin: http://exemplo.com');
 header('Access-Control-Allow-Origin: https://www.site2.com');
 header('Access-Control-Allow-Origin: http://www.site3.com');

Or if you want to enable all use:

header('Access-Control-Allow-Origin: *');
    
02.06.2016 / 14:59
-1

Joseph,

I do not know if it's your knowledge, but it's something server side, which in your case is PHP. That is, it will not be in the HTML that you will correct, but in PHP. This is a matter related to the CORS or Cross-origin resource sharing. Please read the link I left.

By giving my explanation to help you in understanding, a web application can freely upload images, stylesheets, scripts, iframes, videos and plugins like Adobe Flash from any domain. However, web fonts and requests in Ajax are limited only to within the domain itself, and in your case, probably from an X domain you are either making an Ajax request for domain Y or loading the source of a Z domain.

This occurred once with me when a web application, which was in a domain ( link ), I performed an Ajax request on a web service that was in a different domain ( link ).

I have resolved by adding only the header of my web service the permission for this type of request. In my case it was Java, but since you are making use of PHP, the solution will be to add the following line of code:

header('Access-Control-Allow-Origin: *'); 

In the above code, we are releasing to requests from any and all domain, because, as is common in our world, * indicates something like ALL . If you want to specify which specific domains you can request, just replace * with the address like this:

 header('Access-Control-Allow-Origin: http://meusite.com', false);
 header('Access-Control-Allow-Origin: http://outrosite.com', false);
 header('Access-Control-Allow-Origin: https://www.maisum.com', false);

I hope to have helped with something, anything leave a comment!

    
12.05.2017 / 13:56