-1

I am a novice to PHP, trying to learn.

I have my php file present in www folder in the WAMP server.

<?php

    echo 'Hi';
?>

This can be run if I go http://127.0.0.1/testRequestParameter.php from my Browser, it prints Hi

So now I created an HTML page(not present in the same directory)

<html>
<head>
    <script src="jsLibrary/jquery-1.11.1.min.js" ></script>
</head>
<body>
<script type="text/javascript"> 
    function getTestDataFromAjax()
    {
    var url =   'http://127.0.0.1/testRequestParameter.php';

    $.ajax({
        url: url,
        success: function(data) {
          alert(data);
        },
        async:false
      });

    }
</script>

    <input type="submit"  name="Button" onclick="javascript:getTestDataFromAjax(); return false;" />
</body>
</html>

And when I try to call that php through AJAX, the response is blank.

May be it I am missing something, any help will be appreciated.

Finding1: In my firebug it is showing, Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1/testRequestParameter.php. This can be fixed by moving the resource to the same domain or enabling CORS.

Any setting which I need to change?

Sashi Kant
  • 13,277
  • 9
  • 44
  • 71

1 Answers1

0

Scenario

I tested your script using EasyPHP: I created into http://localhost/script/test/ the file content.php

<?php
echo 'Hi!';
?>

Then, I added to my desktop index.php:

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript"> 
    function getTestDataFromAjax()
    {
    var url =   'http://localhost/script/test/content.php';

    $.ajax({
        url: url,
        success: function(data) {
          alert(data);
        },
        async:false
      });

    }
</script>

    <input type="submit"  name="Button" onclick="javascript:getTestDataFromAjax(); return false;" />
</body>
</html>

Then, I launched index.php and clicked on the button, which returned an error when clicked:

XMLHttpRequest cannot load http://localhost/script/test/content.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 

Solution

So, I edited my content.php like this:

<?php
header("access-control-allow-origin: *");
echo 'Hi!';
?>

And it's now working

  • I think you kept both the file in the same directory, My php file is in the WAMP directory inside the "www" folder, but the index file is on desktop – Sashi Kant Aug 31 '14 at 17:43
  • You're right; I'm going to test your scenario and edit my answer –  Aug 31 '14 at 17:45
  • That will be very nice of you :) – Sashi Kant Aug 31 '14 at 17:47
  • By the way, I noticed in my firebug, I got this: `Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1/testRequestParameter.php. This can be fixed by moving the resource to the same domain or enabling CORS.` – Sashi Kant Aug 31 '14 at 17:47
  • Yeah, I noticed about it in chrome console too, the problem was in headers- check my edited answer :) –  Aug 31 '14 at 17:52
  • That is great, Let me check :) – Sashi Kant Aug 31 '14 at 17:57