2

I use this package and it didn't work (https://github.com/barryvdh/laravel-cors)

enter image description here

addSMS() {
    axios.post('https://smsmisr.com/api/webapi', {
                headers: {
                        'Access-Control-Allow-Origin': '*',
                        'Access-Control-Allow-Methods': 'POST, GET, PUT, OPTIONS, DELETE',
                        'Access-Control-Allow-Headers': 'Access-Control-Allow-Methods, Access-Control-Allow-Origin, Origin, Accept, Content-Type',
                        'Content-Type': 'application/json',
                        'Accept': 'application/json'
                    },
                username: '*****',
                passowrd: '****',
                etc: '',
            })
            .then(function (response) {
                console.log(response);
            })
            .catch(function (error) {
                console.log(error);
            });
        }

please tell me how to fix it it's cors not working for me.

3 Answers 3

7

As you have no access in API's source code so there is nothing to do with .htaccess or adding 'Access-Control-Allow-Origin': '*', in script. But still you can eradicate this error by opening chrome browser with disabled security mode

In windows run CMD (press together windows button and R key from keyboard. Then type cmd in box and enter).

Secondly go to C drive (in CLI) or where your chrome installed (using cd command) then run following command

“Program Files (x86)\Google\Chrome\Application\chrome.exe” –-allow-file-access-from-files --disable-web-security --user-data-dir --disable-features=CrossSiteDocumentBlockingIfIsolating

It will open your browser in disabled web security mode. Once you done it you can test now with this kind of CORS or CORB error.

note: before running this command make sure there are no chrome are opened already.

Program Files (x86)\Google\Chrome\Application\chrome.exe this should be your chrome installed path

However after opening browser it will show as below, dont panic & dont close this message. if you close this message it will again through this CORS or CORB error enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

I will try it but maybe it will not solve my problem this api work on postman as well
@AhmedMamdouh yes it will work postman even at live srrver when you add https. But this solution for localhost & development server
you mean when i upload my server it will work fine ?
it's working fine thank you, but i have question when i see in youtube all use localhost and it working with cors easy why i get this problem.
@AhmedMamdouh Perhaps in earlier version of chrome it works but there are a security issue & latest version dont allow to do that
1

Changing

'Content-Type': 'application/json',

to the following will solve the problem:

'Content-Type': 'text/html; charset=UTF-8',

Comments

-1

Update config/cors.php

   'supportsCredentials' => false,
   'allowedOrigins' => ['*'],
   'allowedHeaders' => ['*'],
   'allowedMethods' => ['*'], // ex: ['GET', 'POST', 'PUT',  'DELETE']
   'exposedHeaders' => ['*'],
   'maxAge' => 0,

   then run
   php artisan config:cache 
   command i think it will help you

1 Comment

I do it and nothing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.