Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

socieboy's avatar

CORS and VueJS.

Hi, I'm using VueJS to do a get request to another domain but I'm getting the error of CORS.

XMLHttpRequest cannot load http://xxx.xxx.xxx.xxx:94/. Response to preflight request doesn't pass access control check: A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://myvirualdomain.dev' is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute.

I set some parameters on the Vue global headers but I can't make it works yet. Some help please.

Vue.http.options.xhr = {withCredentials: true};
Vue.http.headers.common['Authorization'] = 'Basic ' + btoa("username:password");
0 likes
39 replies
socieboy's avatar
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
d3xt3r's avatar

No 'Access-Control-Allow-Origin' header is present on the requested resource.

Is your server, setting the CORS headers ???

socieboy's avatar

It's not my own server, I try to access to IP video camera. @d3xt3r They have a API to connect to the camera. I can do that with Guzzle in Laravel, but when I try with Vue I got the error.

d3xt3r's avatar

Its time to read more about CORS, its not something that you can control from your server.

The browser controls this feature. Yes, you might be able to use guzzle to access their api, but if they are not setting the headers appropriately, it might not be possible for you to access it from within you browser page.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

A simple extract

For security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts. For example, XMLHttpRequest follows the same-origin policy. So, a web application using XMLHttpRequest could only make HTTP requests to its own domain. To improve web applications, developers asked browser vendors to allow XMLHttpRequest to make cross-domain requests.

socieboy's avatar

@d3xt3r I really appreciate the time you take to response my friend, so you think it's not possible to have access from the browser directly to the API?

d3xt3r's avatar

No, not unless they allow for cross origin request.

socieboy's avatar

How do I know if the allow for cross origin request? The response headers doesn't have this header.

Access-Control-Allow-Origin

@d3xt3r

d3xt3r's avatar

Then they don't allow for cross-origin request, tough luck.

Edit: Which service is this BTW ?

1 like
socieboy's avatar

What I can do right now It's sent a request to my server and then in the server call the API and it send me the current image of the camera, then i respond with the base64() of the image to Vue and displayed on the site, but it has a delay. the API provide a faststream method, wish keep me update with the most recent image as a video, but if I use this method with guzzle the request is always "pending" because never ends. Do you think there is some way that I can flush the buffer of the request to show on the browser? @d3xt3r

d3xt3r's avatar

I am sorry won't be able to help further. No knowledge on this.

michaelvolst's avatar

I was wonderen what url do you request for? Is it the ip adress of your camera?

Can you tell me the Url?

michaelvolst's avatar

Can you maybe show me some code from vue?

And is it also possible that your using something else from the api? Just grab the image for in example and then get every second a new image?

socieboy's avatar

@michaelvolst

this.$http.headers.common['Access-Control-Allow-Origin'] = 'http://' + camera.ip + ':' + camera.port
this.$http.headers.common['Authorization'] = 'Basic ' + btoa(camera.username + ':' + camera.password);

this.$http.get('http://'+ camera.ip + ':' + camera.port +'/cgi-bin/image.jpg')
    .then(function (response) {
}, function(response) {
        console.log(response)
    })

Im using vue resource and doing a simple ajax request.

I know the authorization is passing, but seems like the preflight request doesn't attach the headers; So tired of this method i was looking around and I found I could use JSONP so I tried and seems like the camera servers is responding but now im getting other error.

refused to execute script from because its mime type ('image/jpeg') is not executable.

Any idea?

SamuelPB's avatar

You said you can make it work with Laravel/Guzzle, you could try to proxy your request in order to send an authenticated request through guzzle. You create a /proxy route in laravel and attach the header with the auth. Here is a package you could use to help you: https://github.com/fideloper/TrustedProxy

Maybe you could also set your auth token in the request you send from vue.js . From what I see it seems you do not attach any auth to your get request.

I do not know the auth protocol you need to use for the API you query but here is an example with oauth: https://auth0.com/blog/2015/11/13/build-an-app-with-vuejs/

1 like
theUnforgiven's avatar

Have you tried

Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('value');

That worked for me a little while back.

1 like
socieboy's avatar

@SamuelPB thank you for the advice I will try to do the proxy thing! and yes I add the authenticate token in the global vue header @lstables
I have this code too!

Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('value');
1 like
SamuelPB's avatar

@socieboy yes but I meant the the Authorization header to authenticate you against the API. You typically would do something like:

$client = new GuzzleHttp\Client(['defaults' => ['headers' => ['Authorization' => 'Bearer'.' '.$token]]]);

It seems I did not see this your js. Maybe the API rejects your calls as you are not authenticated?

socieboy's avatar

Yeah I think this part do the authenticate with the API.

this.$http.headers.common['Authorization'] = 'Basic ' + btoa(camera.username + ':' + camera.password);

@SamuelPB

Next

Please or to participate in this conversation.