0

Following https://github.com/SimulatedGREG/electron-vue, i run yarn run dev and make a minor change to see how it works. In electron vue application, i have launch an ajax request in vue component created hook function,

created: function () {
        let self = this
        this.$http.get('http://example.com/api/hwid/383').then(
                function (resp) {
                    self.title = resp.title
                }
        )
    } 

In the vue-electron dev tool, there are following error in the console:

XMLHttpRequest cannot load http://example.com/api/hwid/383. No 'Access- Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9080' is therefore not allowed access.

How to solve that? Must i set the cross domain in the server side?

0

1 Answer 1

1

Yes, you should add Access-Control-Allow-Origin for localhost on the server side.

Since it's only a browser policy, you eventually can write your own (proxy) server which will get http://example.com/api/hwid/383 data. Then you will request data through your server without any issues.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks. What do you mean "you eventually can write your own (proxy) server"? Do you mean the vue component ajax to my proxy server and my proxy server launch the real ajax data fetching to example.com and return it to the vue component? And the proxy server code is implemented in nodejs running in electron environment?
@kidsit Yep sth like this. When you have your own server (node, php, whatever) you can make any request you want. Then you call your server from vue app.
currently, i have the example.com end point ready(writen in php). my question here is: i am building an electron desktop app, should i write a small proxy script running in electron which proxy the request to the example.com(no access-control-allow-origin limitation since node is on the server side) and forward the response to vue app? In this scenario, vue app which run in chromium will not have the access-control-allow limitation also? So, what i need to do is write that node proxy script? If so, is there any known small script ready for that function? Thanks
If you're controlling exmaple.com backend just add allow-origin to the response headers, anything else will be an overhead.
i do not want to allow-origin to not known domain origin due to security concern. Any more idea on this specific requirement? thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.