简体   繁体   中英

Ajax request for .php script being cancelled

I am trying to run a script via an ajax call in jquery; however, I am getting no result and seeing a "(canceled)" status in chrome's network view. This only happens when I request the .php script, but works fine if I request just a .txt file for example; I get an alert of one\\ntwo\\nthree when I change the url to "test.txt" in by test.html file below. My webserver is configured correctly and permissions of files are all appropriate. If I request my .php script from my browser, it returns "hello" as expected. Below are my three files, why do I get a cancelled request for test.php but not for test.txt ?

test.html

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <link rel="shortcut icon" href="/favicon.ico">
      <script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
      <title>test</title>
   </head>
   <body>
      <button id="b">click</button>
      <script>
         $("#b").bind("click", function() {
            $.ajax({
               url: "test.php", // test.txt works fine
               type: "GET",
               cache: false,
               success: function(data) {
                  alert(data);
               }
            });
         });
      </script>
   </body>
</html>

test.txt

one
two
three

test.php

<?php
echo "hello";
?>

As mentioned, I get the correct "hello" output if I request test.php with my browser so it is executing fine.

-rwxr-xr-x 1 me me 22 Oct 1 02:49 test.php

(Canceled) is frustrating. If you go to chrome://net-internals/#events you can see in more detail why your request is getting (canceled) . There could be a few reasons why your request is being blocked by Chrome, but you'll get more information about your request at that url. If you're still stuck, edit your question to include this information.

Assuming you're using WAMP or LAMP.

If your test.html file is outside of the web server's www directory, the web server will prevent access to your test.php file because it violates the same origin policy . This would explain why visiting the test.php file in the browser works fine, and also why this test.html's requests to test.php file are failing.

It may not be this simple, but this is something that gets me every once in a while as well. Especially when I have an index.html laying around on the desktop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM