当前位置: 首页 > 知识库问答 >
问题:

Vuejs和Laravel Post Request CORS

林英朗
2023-03-14
methods: {
            chargeCustomer(){
                this.$http.post('/api/chargeCustomer', this.payment).then(function (response) {
                    console.log(response.data)
                },function (response) {
                    console.log(response.data)
                });
            }
        }
Route::group(['middleware' => 'cors'], function(){
    Route::post('/api/chargeCustomer', 'Backend\PaymentController@chargeCustomer');
});
Vue.http.headers.common['Access-Control-Allow-Origin'] = '*';
Vue.http.headers.common['Access-Control-Request-Method'] = '*';

有人能告诉我我做错了什么吗?

共有1个答案

杜成和
2023-03-14

您需要从中间件设置CORS报头。也许你需要一些额外的设置?

无论如何,您可以创建自己的中间件,并在handle()方法中设置CORS头,如下所示:

public function handle($request, Closure $next) 
{
    return $next($request)
           ->header('Access-Control-Allow-Origin', 'http://yourfrontenddomain.com') // maybe put this into the .env file so you can change the URL in production.
           ->header('Access-Control-Allow-Methods', '*') // or specify `'GET, POST, PUT, DELETE'` etc as the second parameter if you want to restrict the methods that are allowed.
           ->header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Authorization') // or add your headers.
}

将自定义中间件添加到kernel.php类中的全局$middleware数组(位于checkformaintenanceMode::class下),您就可以开始工作了。

 类似资料:
  • Framework7-VueJS Use for auth: demo/demo This app shows you example of using mobile framework - Framework7 with VueJS, Vuex, vue-router, vue-i18n to build 'personal account' application. Features webp

  • Flask-Vue.js �� compatible Vue.js v2 �� confrict a Jinja2 delimiter and Vue.js delimiter... => change Jinja2 delimiter from flask import Flaskclass CustomFlask(Flask): jinja_options = Flask.jinja_opt

  • Project Title Slooots Project Description slot machine created with vuejs, the mobile version is created with nativescript-vue and both web and mobile contain simple logic, sounds and a spinning reels

  • 问题内容: 这个问题与尚未使用的intl-tel-input和vuejs2一起使用相似。 因此,长话短说,我正在动态设置一个新的引导程序选项卡(标题和URL),然后尝试使用来(重新)绑定某些功能。 在我的Vue方法中添加以下行https://github.com/thecodeassassin/bootstrap-remote-data/blob/master/js/bootstrap-remot

  • 我想更改一个数据属性,并在Laravel中的Vue实例上运行一个方法。然而,由于使用webpack和laravel,我似乎无法访问实例,我希望如何: 所以似乎不是我的Vue类的正确实例。 下面是我正在加载的刀片视图,正如您所看到的,我在main中添加了一个脚本标记,只是试图更改Vue实例数据属性,并运行一个方法。 下面是我(资源/资产/js/app.js)的一部分: 如您所见,我希望通过我在中定义

  • Flask Vue.js全栈开发 测试地址: http://120.77.33.143 RESTful API: http://120.77.33.143:5000/api/posts (用 Firefox 查看返回的 JSON 数据更佳),或者将仓库内的 flask-vuejs-madblog.postman_collection.json 导入你的 Postman 工具 本系列的最新代码及使用