简体   繁体   English

laravel vuejs socket.io Access-Control-Allow-Origin

[英]laravel vuejs socket.io Access-Control-Allow-Origin

hello laravel i am trying to use socket.io with vuejs but i keep getting errors, i couldn't understand where i went wrong.你好 laravel 我正在尝试将 socket.io 与 vuejs 一起使用,但我不断收到错误消息,我不明白我哪里出错了。 What I want to do is clearly when the form is submitted, it is listened to by the audience and instantly sees the post to the manager.我想要做的很清楚,当表单提交时,它被听众听到并立即看到经理的帖子。 Of course, this is not happening.当然,这不会发生。 I am using socket.io wrong.我使用 socket.io 错了。 I couldn't understand.我无法理解。

laravel.local/:1 Access to XMLHttpRequest at 'http://localhost:3000/socket.io/?EIO=3&transport=polling&t=NNy_ruN' from origin 'http://laravel.local:8000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

server.js服务器.js

var app = require('express')();
const server = require('http').createServer();
const io = require('socket.io')(server);


// http.listen(3000);

app.get('/', (req, res) => {
    res.send('<h1>Hello world</h1>');
});

io.on('connection', socket => {
    socket.on('new_appointment_create', function () {
        console.log("okey");
        io.emit('admin_appointment_list');
    });
});

server.listen(3000, function () {
    console.log('Listening on Port: 3000');
});

formpost.vue formpost.vue

import io from 'socket.io-client';
var socket = io('http://localhost:3000');



.then((response) => {
                    if (response.status) {
                        socket.emit('new_appointment_create');
                        this.completeForm = false;
                    }
                })

adminlist.vue管理列表.vue

import io from 'socket.io-client';
var socket = io('http://localh
created() {

        this.getData();
        socket.on('admin_appointment_list', () => {
            console.log("list okey");
            this.getData();
        });
    },

package.json包.json

"devDependencies": {
        "axios": "^0.19",
        "bootstrap": "^4.0.0",
        "cross-env": "^7.0",
        "jquery": "^3.2",
        "laravel-mix": "^5.0.1",
        "lodash": "^4.17.19",
        "popper.js": "^1.12",
        "resolve-url-loader": "^3.1.2",
        "sass": "^1.20.1",
        "sass-loader": "^8.0.0",
        "socket.io": "^3.0.3",
        "socket.io-client": "^2.3.1",
        "vue": "^2.5.17",
        "vue-template-compiler": "^2.6.10"
    },
    "dependencies": {
        "express": "^4.17.1",
        "laravel-vue-pagination": "^2.3.1",
        "v-mask": "^2.2.3",
        "vue-resource": "^1.5.1"
    }

The error you're receiving is due a feature from browsers called preflight that consists in the browser doing kind of "ping request" to the server that you want to access with an HTTP OPTIONS to get the response headers and check for the Access-Control-Allow-Origin header.您收到的错误是由于来自浏览器的一项名为preflight的功能,该功能包括浏览器向您要使用 HTTP OPTIONS 访问的服务器执行某种“ping 请求”以获取响应标头并检查Access-Control-Allow-Origin标头。

The idea is that your API should declare in this header which origins(domains) are allowed to consume your resources/APIs.这个想法是你的 API 应该在这个标头中声明哪些来源(域)可以使用你的资源/API。

The "easiest" way to resolve this is to configure your API to respond with Access-Control-Allow-Origin: * .解决此问题的“最简单”方法是配置您的 API 以使用Access-Control-Allow-Origin: *进行响应。 But it's not the correct way, you shouldn't go to production * as configuration.但这不是正确的方法,您不应该将生产*作为配置。

In my opinion the correct way to resolve this is use a http proxy while in dev, and in production you configure your Apache or NGINX or whatever http server you'll use to do so.在我看来,解决此问题的正确方法是在开发中使用 http 代理,在生产中配置 Apache 或 NGINX 或用于执行此操作的任何 http 服务器。

If you're using @vue/cli , it already has an option to configure a proxy server for development, take a look in the docs .如果您正在使用@vue/cli ,它已经有一个选项来配置用于开发的代理服务器,请查看文档

But fastforwarding the refactoring needed if you use the devProxy option, you will basically map an resource/context in the same server/port which is served you front-end to proxy requests/connections to your websocket.但是,如果您使用devProxy选项,则需要快速进行重构,您基本上将在同一服务器/端口中映射资源/上下文,该服务器/端口为您的前端提供代理请求/连接到您的 websocket。

Then you'll stop calling the http://localhost:3000 directly and will use the mapped resource/context.然后您将停止直接调用http://localhost:3000并使用映射的资源/上下文。

For example:例如:

In your vue.config.js在你的 vue.config.js

module.exports = {
  devServer: {
    proxy: {
      '^/api': {
        target: 'http://localhost:300',
        ws: true
      }
    }
  }
}

Then you will create the socket:然后您将创建套接字:

var socket = io(`${location.protocol}//${location.host}/api`);

Being honest with you, I've never used the devProxy with websockets, but the docs says it has support for it.老实说,我从未将devProxy与 websockets devProxy使用,但文档说它支持它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Socket.io-Access-Control-Allow-Origin不允许的来源 - Socket.io - Origin not allowed by Access-Control-Allow-Origin 没有“访问控制允许来源”-节点/反应/socket.io - No 'Access-Control-Allow-Origin' - Node / react / socket.io socket.io, &#39;Access-Control-Allow-Origin&#39; 错误 - socket.io, 'Access-Control-Allow-Origin' error Socket.io 访问控制允许来源通配符 - Socket.io Access-Control-Allow-Origin Wildcard Socket.io与本地客户端。 Origin null和Access-Control-Allow-Origin - Socket.io with local client. Origin null and Access-Control-Allow-Origin Socket.io跨域问题 - Access-Control-Allow-Origin不允许使用Origin - Socket.io cross-domain issue - Origin is not allowed by Access-Control-Allow-Origin Socket.io + Nodejs + Angular2 +-缺少CORS标头&#39;Access-Control-Allow-Origin&#39; - Socket.io + Nodejs + Angular2+ - CORS header 'Access-Control-Allow-Origin' missing express + socket.io + kubernetes Access-Control-Allow-Origin&#39;标头 - express + socket.io + kubernetes Access-Control-Allow-Origin' header 响应中的 Access-Control-Allow-Origin 标头不能是通配符 &#39;*&#39;... Socket.io、NodeJs、ReactJs - Access-Control-Allow-Origin header in the response must not be the wildcard '*'... Socket.io, NodeJs, ReactJs node express 和 socket.io 中的 CORS 配置没有“Access-Control-Allow-Origin” - CORS config in node express and socket.io No 'Access-Control-Allow-Origin'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM