2

After awhile of not using JSON, I'm a little rusty on the possibility of requestiing the data from another domain/web-server.

<?php header('Access-Control-Allow-Origin: *');
  if(!empty($_GET['file'])){
?>
  jsonFile = 'data/<?php echo $_GET['file']; ?>';
<?php
  }
?>

Is there a way to do this without using JSONP? I think the way I have my code setup, it queries from a data/ folder. That data folder is also available on the other web server. Any thoughts? All appreciated.

2
  • You will most likely encounter CrossBrowser scripting error with that ... Which will result in an error Commented Sep 30, 2018 at 4:20
  • @IamCavic What would you recommend? Commented Sep 30, 2018 at 4:24

1 Answer 1

3

You don't need that header here. Presence of Access-Control-Allow-Origin matters only when you are requesting a resource from another domain via browser. And it's the target server that should return the header not the client. Since you are using PHP you just need to request the resource normally.

Check the MDN's CORS article for more information.

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

Comments

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.