0

EDIT: Cavid Kərimov's answer does work. Setting the form as below does also work.

<form onsubmit="return false;"></form>

I'm trying to retrieve a simple string of text from a PHP file using jQuery and AJAX as shown below:

index.html

$.get("echo.php", function(data, status) {
    console.log("success: " + data);
}).fail(function(jqXHR, textStatus, error) {
    console.log("failed: " + JSON.stringify(jqXHR));
});

echo.php

<?php

    echo "echoed from php successfully";

?>

But I'm getting the following error:

failed: {"readyState":0,"status":0,"statusText":"error"}

I googled around for a bit and apparently this happens because of the Same-origin Policy, however I'm sure that isn't the case here because the HTML and PHP file are stored in the exact same folder on the server.

server directory (can't post images because it requires 10 reputation)

  • 3
    Have you tried to just open php file in your browser? – Senad Meškin Dec 08 '17 at 17:43
  • Inspect your network requests (in something like Chrome Devtools). What is it telling you? Can you add a picture of that? – Gabe Rogan Dec 08 '17 at 17:43
  • Good point @SenadMeškin in that case see [this question](https://stackoverflow.com/questions/3004696/is-there-a-simple-php-development-server) – Gabe Rogan Dec 08 '17 at 17:45
  • also log the error message fail(function(jqXHR, textStatus, error) { – siddhesh Dec 08 '17 at 17:46
  • 1
    open your echo.php in browser and see what it shows you – Javid Karimov Dec 08 '17 at 17:46
  • just the fact that you do not include a path to a different domain confirms you are not looking in a different origin. I would try a simple call like `... function(d){ console.log(d)}` and see what you get, AFTER opening echo.php in a browser first. – NappingRabbit Dec 08 '17 at 17:46
  • @GabeRogan I'm doing a college project and the teacher gave us an actual working server somewhere in the US which obviously I have no access to except for a FTP account and a database. – Martim Ferreira Dec 08 '17 at 17:49
  • [jQuery Ajax - Status Code 0?](https://stackoverflow.com/questions/2000609/jquery-ajax-status-code-0) – Andreas Dec 08 '17 at 17:50
  • @Senad Meškin Yes I tried that and it works. The browser correctly displays the echo string. – Martim Ferreira Dec 08 '17 at 17:51
  • @Andreas I think that may be it. It does get redirected. doc ready clicked id: 1 Navigated to http://www.sportsinitiative.org/TPSIB17/p7/ajax-test/ failed: {"readyState":0,"status":0,"statusText":"error"} – Martim Ferreira Dec 08 '17 at 17:52
  • @MartimFerreira Your teacher wants you to upload your finished project to that server. While you're developing you need a PHP server on your computer (usually accomplished with `php -S localhost:8000` or software like MAMP or WAMP) – Gabe Rogan Dec 08 '17 at 17:53
  • @GabeRogan The directoy showed in the picture is the actual server directory. I did think about that though. But wouldn't it be pointless if it only worked locally? – Martim Ferreira Dec 08 '17 at 17:53
  • @Andreas Checked out your link, fixed. I'll post the solution in a second. – Martim Ferreira Dec 08 '17 at 17:57

1 Answers1

0

Use this and tell us what happened:

         $("#go").click(function() {
                console.log("clicked");

                var $id = $("#id").val();
                console.log("id: " + $id);

                $.get("echo.php", function(data) {
                    console.log("success: " + data);
                }).fail(function(data) {
                    console.log("failed: " + data);
                });

             return false;                    
            });
Javid Karimov
  • 385
  • 4
  • 19