Data is empty in jQuery.get () callback

$.get("http://localhost/test.php", (function(data){
    alert(data);
}))

The warning is empty. I am new to this and I do not see what I am doing wrong. Help?

+3
source share
7 answers

It works with URLs file://but not http:// ? I smell the same origin policy .

Do you open a jQuery file in your browser through a URL file://and then try to extract it through http://? Because your protocol and host must match when you do Ajax.

Make sure that you open the file in your browser as http://localhost/index.phpwell as not file://localhost/index.php . Then you can get this file through http://localhost/test.php.

+3

, , ? $(document).ready()

$(function() {
  $.get("http://localhost/test.php", function(data){
    alert(data);
  });
});

http://api.jquery.com/ready/

+2

http://locahost/test.php , - ?

, jquery url , .

0

test.php -?

.

0

mime?

, XML, , JavaScript JSON, MIME . .

, , , .

0

The callback function should be the third parameter in $ .get:

 $.get("http://localhost/test.php", {}, (function(data){
     alert(data);
 }))
-1
source

Well, I decided: it works when the html file is in the web server directory and is called from localhost, it does not work when the html file is: //. This is inconvenient, but good. (It seems that I can not mark my own answer as a solution up to 2 days.)

-1
source

Source: https://habr.com/ru/post/1760798/


All Articles