带有vue.js和laravel的CORS无法使用 [英] CORS with vue.js and laravel won't come to work

查看:104
本文介绍了带有vue.js和laravel的CORS无法使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望您可以帮助laravel和vue.js newby.我正在编写一个小应用程序,需要通过darksky.net调用weather api.

I hope you may help a laravel and vue.js newby. I am writing a little application where I need to call the weather api by darksky.net.

通过php效果很好.现在,我希望通过vue.js更动态地进行操作.但是,如果我通过axios进行api调用,则会收到以下消息:

Via php it works well. Now I want it more dynamically with vue.js. But if I make an api call via axios, I get the following message:

无法加载> https://api.darksky.net/forecast/myDarkSkyKey/37.8267,-122.4233 :对预检请求的响应>未通过访问控制检查:所请求的资源上不存在"Access-Control-> Allow-Origin"标头.因此,不允许访问来源>' http://localhost:5757 '.

Failed to load > https://api.darksky.net/forecast/myDarkSkyKey/37.8267,-122.4233: 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:5757' is therefore not allowed access.

我大量介绍了cors,并尝试将标头包含在中间件中,如" laravel-cors "由berryvhd 打包,但我一次又一次地反对不存在'Access-Control-> Allow-Origin'标头"墙.

I red a lot stuff about cors and tried to include the headers with middleware as explained here, and I even tried the "laravel-cors" package by berryvhd, but I am running against that "No 'Access-Control-> Allow-Origin' header is present" wall again and again.

您有任何建议或线索我在做什么错吗?我做了很多尝试,甚至包括了这些

Do you have any advises or a clue what I am doing wrong? I tried that much, even including these

header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS');

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');

直接进入public/index.php

directly into public/index.php

我知道这不是很好的方法,但是我想弄清楚什么是行得通的.但是没有帮助.

I know its not the fine way, but I wanted to figure out whats working. But nothing helps.

我正在使用最新的laravel master和vue.js 2,MAMP和CodeKit.

I am working with the latest laravel master and vue.js 2, MAMP and CodeKit.

推荐答案

感谢您的想法! 我找到了适合我的解决方案:代理服务器,即 https://cors-anywhere.herokuapp.com/

Thanks for your thoughts! I found a solution working for me: proxy servers i.e. https://cors-anywhere.herokuapp.com/

所以我可以向Darksky发出我的请求,如下所示: https://cors-anywhere .herokuapp.com/https://api.darksky.net/forecast/myDarkSkyKey/37.8267,-122.4233

so I can do my request to darksky as follows: https://cors-anywhere.herokuapp.com/https://api.darksky.net/forecast/myDarkSkyKey/37.8267,-122.4233

那对我有用.只有一种不好的味道->如果herokuapp服务器宕机了怎么办!?

That will work for me. Only one bad taste -> what if the herokuapp server is down!?

这篇关于带有vue.js和laravel的CORS无法使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆