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

function getValues(){

   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))); 


You can't do that, you should open your html file also from web server address eg http://localhost/yoursite/file.html or even remote server url. You need to go through the server/server url.


AJAX needs a webserver to communicate with for it to be able to retrieve any data; otherwise its just talking to a wall. Running the script without a webserver is like trying to make a call with no cell-service. :D

+3  A: 

The web server is exactly what is handling all of the details for you.

You cannot POST without a web server to post to. HTTP = web protocol, so you cannot have a HTTP URL without a web server to target.

The web server is also the process that takes your request for a PHP page and runs the PHP interpreter, managing the inputs and outputs.

Why do you want to run it locally?

I think he is running the web page locally, (via C:\...\index.html) but he is still making the request to the web server (with php). I think it is a cross site scripting issue (see my answer), not a parsing issue.
Bob Fincheimer
yes exactly as Bob says

the URL part


means that an HTTP server must reply to your request. Since no server is running (like wamp, iis etc.) the reply is null. Nothing is unexpected.


Read the SOP. Accessing data from a domain other than the current one is blocked for security reasons.

I don't think this is relevant to his problem.
Kirk Woll

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.

Bob Fincheimer
I did not know this. thank you

Ajax does not work over the file:// protocol as mentioned by others. Perhaps you want something like to create desktop apps with html/js/css