link685 link686 link687 link688 link689 link690 link691 link692 link693 link694 link695 link696 link697 link698 link699 link700 link701 link702 link703 link704 link705 link706 link707 link708 link709 link710 link711 link712 link713 link714 link715 link716 link717 link718 link719 link720 link721 link722 link723 link724 link725 link726 link727 link728 link729 link730 link731 link732 link733 link734 link735 link736 link737 link738 link739 link740 link741 link742 link743 link744 link745 link746 link747 link748 link749 link750 link751 link752 link753 link754 link755 link756 link757 link758 link759 link760 link761 link762 link763 link764 link765 link766 link767 link768 link769 link770 link771 link772 link773 link774 link775 link776 link777 link778 link779 link780 link781 link782 link783 link784 link785 link786 link787 link788 link789 link790 link791 link792 link793 link794 link795 link796 link797 link798 link799 link800 link801 link802 link803 link804 link805 link806 link807 link808 link809 link810 link811 link812 link813 link814 link815 link816 link817 link818 link819 link820 link821

[Vue.js] 405 Error & CORS when making an api request on localhost

I’m trying to pull data from a weather API on my localhost environment and I’m getting a 405 Error(Method not allowed) as well as “has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: It does not have HTTP ok status.” I’ve config’d my headers and can’t figure out what i’m doing wrong. It works fine on live but obviously that’s not a convenient way doing it.

I’ve tried using axios.get as well as axios.post.

var config = {
headers: {‘Access-Control-Allow-Origin’: ‘*‘}
};
getWeather: function (){
axios.get(‘http://api.openweathermap.org/data/2.5/forecast?id='+ city + ‘&appid=’+ apiKey, config).then((res) => {this.weather = res.data});
}

Solution :

Hope adding below package will help

use this https://github.com/barryvdh/laravel-cors

Installation :

composer require barryvdh/laravel-cors

Service provider :

Add this Barryvdh\Cors\ServiceProvider::class, in config/app.php

To allow CORS for all the routes, add the HandleCors middleware in the $middleware property of app/Http/Kernel.php class:

protected $middleware = [
\Barryvdh\Cors\HandleCors::class,
// …
];

NOTE: make sure to place it before all middlewares

If it’s for specific middleware, on our case, it will be api

protected $middlewareGroups = [
‘web’ => [
// …
],

‘api’ => [
\Barryvdh\Cors\HandleCors::class,
‘throttle:60,1’,
‘bindings’,
],

];

NOTE: make sure to place it before all middlewares

Publish the config :

php artisan vendor:publish –provider=”Barryvdh\Cors\ServiceProvider”