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.

2
  • 2
    Possible duplicate of jQuery AJAX cross domain Commented Jan 18, 2017 at 12:43
  • i want to achieve it without Json Commented Jan 18, 2017 at 12:48

2 Answers 2

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

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

1 Comment

Is this safe to use?
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 Comment

You are also correct first saw that one so accepted it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.