简体   繁体   中英

Cors Issue In Laravel 9 / Axios

I'm trying to upload a file using Axios but Im getting an error I tried everything, It works fine on Postman ( I can upload ), the problem only in the browser

This is my cors.php file :

    'paths' => ['api/*'],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => ['*'],

    'max_age' => 0,

    'supports_credentials' => false,

This is my Axios request:

selectedFile: function(){

            let form = document.getElementById('myform');
            let data = new FormData(form);
            data = data.append('file', document.querySelector(".dropzoneFile").files[0]);
  
           axios.post("http://testing.localhost/api/lab/",
                data,
                    {
                        headers: {
                            "Content-Type": "multipart/form-data",
                            "Authorization": "Bearer 186|k28yQNGcJcjZAi7k2ilYR0MUxh8pljMy56JOH4QS",
                            "Accept": "*/*",
                            "Access-Control-Allow-Origin": "*"
                        }
                    }
                )
                .then(function (response) {
                    //handle success
                    console.log(response);
                })
                .catch(function (response) {
                    //handle error
                    console.log(response);
            });

        }

This is my Route inside Api.php Protected by sanctum:

Route::post('/lab', function (Request $request) {
        dd($request->all());
    });

Result :

Request URL: http://testing.localhost/api/lab/
Request Method: POST
Status Code: 500 
Referrer Policy: strict-origin-when-cross-origin

By the Way All my normal forms works fine but that one because I'm uploading a file And It works fine when I use Postman, the problem only in the browser

Found it by my self : NEVER AND NEVER USE dd(); While you are using API. because of the preflight should not return an HTTP error, and that's what dd() caused

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM