Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

php - Jquery: Running AJAX locally without a webserver

I have the following function in a .js file in index.html

function getValues(){

 $.ajax({
   type: 'POST',
   url: "http://localhost/getData/getdata.php",
   success: function(data){
     var dataValues;
     var apnd;

     dataValues = String(data.NSE);
     apnd = "a";
     updateValues(dataValues, apnd);

     dataValues = String(data.BSE);
     apnd = "b";
     updateValues(dataValues, apnd);
    },
   dataType: "json"
 });

}

this works fine when I run it in a webserver like wamp. But I want to run index.html locally i.e without a webserver, The user just double clicks index.html and it should run but it doesn't. data is always null. What could be the problem? Sorry I am a super JQuery Noob.

the code in getdata.php file is

<?

echo json_encode(array("NSE"=>rand(5000, 20000),"BSE"=>rand(5000, 20000))); 

?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When you run your index.html from a file the AJAX works. But the problem occurs because you are viewing the file at address "file://....../index.html" and you are making a AJAX request to "http://localhost/..../something.php" which IS NOT ALLOWED because of cross site scripting. All AJAX requests must go to the same domain/server.

This is a assuming that you are viewing the file by double clicking it and still making the AJAX request to the web server.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...