0

I need to fetch the data from one of my files located at different url.Here is the code that shows the ajax request sent to the server

<script>
    alert('return sent');
    $.ajax({
        type: "POST",
        url: "example.com/show.php",
        data: 1
    })
    success: function data(response) {
        alert(data); // apple
    }
</script>

Here is the code on another file that I am accessing through Ajax (example.com/show.php).

<?php
    echo 'Hello '; 
?>

However I am getting Cross-Origin Request Blocked: warning in my console.

Hristo Eftimov
  • 13,845
  • 13
  • 50
  • 77

2 Answers2

1

add in show.php

header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Content-Type, *");
1

Please replce your show.php like

<?php
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Content-Type, *");
echo 'Hello ';

?>

Hope it helps

Pratik bhatt
  • 488
  • 8
  • 23