0

I am developing an HTML/Javascript/JQuery page retrieving a JSON returned by a PHP script:

$.ajax({
url: "data.php",
    dataType: 'json',
success: function(rate) {
        var retr = JSON.stringify(rate, null, 2);
        $("#data").text(retr);
},
    error: function(request,error) {
        alert(request.statusText);
    }
});

When I view the HTML page from my local PC, the Ajax returns the PHP script code rather than the JSON the PHP code should generate. Somehow, it makes sense since it does not go through a PHP engine.

I have tried replacing url: "data.php" with url: "http://www.mysite.com/data.php" or url: "www.mysite.com/data.php", but it does not work.

I would like to avoid uploading my page on a server to test it. I would like to be able to invoke the PHP on my PHP server directly from my local PC. Is this possible?

5
  • May be your web server is not running. Commented Jan 28, 2012 at 5:21
  • 1
    make sure your PHP is enclosed in <?php ?> Commented Jan 28, 2012 at 5:22
  • Is your script between <?PHP and ?>? Commented Jan 28, 2012 at 5:23
  • have you installed php and a webserver on your local machine? Commented Jan 28, 2012 at 5:25
  • If I call mysite.com/data.php directly from my browser, it works... Commented Jan 28, 2012 at 5:28

3 Answers 3

1

you will have to upload the page on the server, otherwise you will be restricted by CORS

add the following line in your php script

<?php
header('Access-Control-Allow-Origin: *');
echo json_encode("hello");
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Any chance I can work around CORS, just for testing purposes?
0

I think this post would help you solve your issue just go through the steps mentioned here.

http://php.about.com/od/troubleshooting/qt/shows_code.htm

Comments

0

Take care if the php file has properly set php tag

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.