-3

I've been trying to get all my code works on my local server. I spend all the time of my life to get rid of errors to make everything run smoothly. Once it works, I uploaded it to my development server ( a place where my codes were play together, eat some meal, then die by the time ).

Today I've done from creating another creatures of life with Jquery, he looks so happy when he was born into the local system and placed to the home I called "localhost". Then, I try to send him to the wild, he was dead even before he was loaded yet. I try to browse who was killed him, and I realized something called CORS is the suspect. Now I does really want to take a revenge, how to kill him ? If it cannot be killed, how can I force killing him ? He kills my creatures, I will never forgive him.

This is how he looks :

Failed to load http://localhost/dailyreport/function/TarikDataAbsensi.php: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://my-creatures-new-home.com' is therefore not allowed access.

  • See how `my-creatures-new-home` is trying to load from `localhost` ... that's not the same origin - one thing to note, your `localhost` is not the same as everyone elses `localhost` - therefore, if you expect this code to work for people other than you, then adding CORS headers in your localhost server will only help your access, not others – Jaromanda X Mar 18 '18 at 22:38
  • does it mean I must re-code the whole thing from the ajax ? or just adding it and let it do the magic ? – Donny Pratama Mar 19 '18 at 02:48
  • not sure what you're asking about "re-coding the whole thing". If you want a cross origin request to work, then the server must send CORS headers in the response headers ... no idea what `just adding it and let it do the magic` even means – Jaromanda X Mar 19 '18 at 02:55
  • ok thanks, I think that's clear, thanks for the solution, I'll figure it out how to do that `adding CORS headers`. Currently, I have tried ajax cors request in this [link](https://www.html5rocks.com/en/tutorials/cors/#toc-cors-from-jquery) but that is only work with plain contentType. – Donny Pratama Mar 19 '18 at 04:58
  • that link is client side code ... again ... the fix must be done on the server as the server determines if cross origin resources are allowed or not – Jaromanda X Mar 19 '18 at 04:59
  • i have no idea about this problem. 7 hours trying to fix it and i can't get it.. i tried to put cors header request both on the client side and the server side (php) inside the requested file and tried to [enable cross origin on apache configuration](https://enable-cors.org/server_apache.html) but it was still blocked. i put the cors allow blablabla on the .htaccess file and it brokes the whole request. any clues please sir ? Edit : honestly, i'm new to javascript, especially in jquery and ajax – Donny Pratama Mar 19 '18 at 05:25
  • `cors header request` ... no, browser adds the cors request headers in the request ... and expects cors response headers in the response - your browser code should not be doing anything specifically with CORS at all – Jaromanda X Mar 19 '18 at 05:43
  • aaargh.. it blows my mind :x – Donny Pratama Mar 19 '18 at 06:00
  • lol.. it works just by changing `header('Access-Control-Allow-Origin: *');` to `header('Access-Control-Allow-Origin: http://localhost:80/');` on the requested file.. now i'm laughing hard at myself. – Donny Pratama Mar 19 '18 at 06:11
  • right, so that's the **server** side - which is what I was saying all along – Jaromanda X Mar 19 '18 at 07:36

1 Answers1

1

Granting JavaScript clients basic access to your resources simply requires adding one HTTP Response Header, namely:

Access-Control-Allow-Origin: *

source

You need to add this header in your server config for the localhost server. For example if you are using nginx you can can add the following to your config

add_header Access-Control-Allow-Origin *;
Community
  • 1
  • 1
Jamie
  • 131
  • 3
  • 1
    You should add how to fix the problem, since it is the original question - even though it may be hard to discern that. – Derek Pollard Mar 18 '18 at 22:03