vue开发中,向服务器请求数据发生跨域,无法得到服务器json数据问题

问题:

在开发一个vue小项目,后台用nodejs搭的,向服务器发送正确的用户名密码会得到一个token,用postman可以正确获得。但是在vue里怎么实现?

postman 截图:

https://img1.mukewang.com/5be69b3b00011db212530319.jpg

Vue 的代码如下:

https://img.mukewang.com/5be69b4a0001e0c606700473.jpg

结果报错是这样的:

https://img3.mukewang.com/5be69b5e0001928915130376.jpg

XMLHttpRequest cannot load http://localhost:8000/api/aut... Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

Uncaught (in promise) Response {url: "http://localhost:8000/api/authenticate", ok: false, status: 0, statusText: "", headers: Headers…}

请问怎么才能做到传一个jsond对象给服务器,然后获取服务器返回的json对象,然后在页面能够获取(渲染)?

如果能提供demo或者代码的话,感激不尽。


皈依舞
浏览 1826回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript