Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Getting CORS error-No 'Access-Control-Allow-Origin' header is present
#1

(This post was last modified: 07-20-2018, 09:11 AM by aslamdoctor.)

I have created an API server using codeigniter. I haven't used any kind of rest api libraries. Just created array of data and did json encoding on it. Now I am trying to fetch the data by making ajax request to this API server but getting this error

No 'Access-Control-Allow-Origin' header is present on the requested resource

This is because the request I am sending is from different server(or different URL).
These both API server and Ajax client app are set up locally only. So is there any way I can bypass this access-control check for development purpose? as the final app is going to be uploaded to same URL and there I don't have to worry about this error.

Thank You,
Aslam
Reply
#2

Cross-Origin Resource Sharing (CORS)

I was get errors  on fonts and had to add this to the .htaccess file.

Code:
# Add Font Types
AddType application/vnd.ms-fontobject .eot
AddType font/truetype .ttf
AddType font/opentype .otf
AddType font/opentype .woff
AddType font/opentype .woff2
AddType image/svg+xml .svg .svgz

<FilesMatch ".(eot|ttf|otf|woff|woff2|svg|svgz)">
    Header set Access-Control-Allow-Origin "*"
</FilesMatch>
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(07-20-2018, 08:40 AM)InsiteFX Wrote: Cross-Origin Resource Sharing (CORS)

I was get errors  on fonts and had to add this to the .htaccess file.

Code:
# Add Font Types
AddType application/vnd.ms-fontobject .eot
AddType font/truetype .ttf
AddType font/opentype .otf
AddType font/opentype .woff
AddType font/opentype .woff2
AddType image/svg+xml .svg .svgz

<FilesMatch ".(eot|ttf|otf|woff|woff2|svg|svgz)">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>

Fixed it. It was not the issue with access control but stupid js was showing error for it.
It was actually happening because I was doing post request using axios and the data I was passing to the ajax request was a js object. It was needed to be stringified using the qs.stringify method. I did it and it got fixed.

Btw, I am using vue js for this app. And here is my final code

Code:
this.$axios({
                        method:'post',
                        url:'http://localhost/hmvc/user/insert',
                        responseType: 'json',
                        data: this.$qs.stringify(vm.formdata)
                    })
                    .then((response)  =>  {
                        console.log(response.data);
                    }, (error)  =>  {
                        console.log(error);
                    });
Reply




Theme © iAndrew 2016 - Forum software by © MyBB