-1

So I have an ajax request:

$.ajax({
  url: "some url different domain/transfer.php",
  headers: {
    "Access-Control-Allow-Origin": "*",
  },
  type: "post",
  dataType: "json",
  success: function (msg) {
    console.info(msg);
    gfg = msg;
    console.log(gfg);
  },
});

and this is the header of my transfer.php file-

<?php
header('content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *");

So after working for more than 2 days I am having an error:

Access to XMLHttpRequest at 'some URL different domain/transfer.php' from origin 'http://localhost' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I have tried also tried "jsonp" instead of JSON type but in that case, it gives me an error: "<? unidentified token in 1st line" which is obviously necessary for PHP.

Pleaseeeeee anyone on this planet can help me to make a post/get request using js to PHP (I am receiving an array from PHP). Before I burn my laptop :) Thanks!

  • 1
    You can try using some of the solutions here, https://stackoverflow.com/questions/8719276/cross-origin-request-headerscors-with-php-headers – waterloomatt Jan 28 '22 at 12:57

1 Answers1

0

Add on your server Access-Control-Allow-Origin: *