I have the following function in a .js file in index.html
function getValues(){
$.ajax({
type: \'POST\',
url: \"http://localhost/getData/getdata.php\
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.
Read the SOP. Accessing data from a domain other than the current one is blocked for security reasons.
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
Ajax does not work over the file:// protocol as mentioned by others. Perhaps you want something like http://www.appcelerator.com/ to create desktop apps with html/js/css
I'm tickled pink with myself because reading the answers people posted about how you can't do AJAX "locally" without a Web server led me to the solution as to how you can do it. With JavaScript, the XMLHttpRequest() object's methods are mostly produced by the browser and you need to leave out the one produced by the Web server (xmlhttp.status == 200). The following works:
<script>
window.onload = function() {
var input = document.getElementById("input");
input.onclick = function() {
var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
document.getElementById("response").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "response.html", true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<h3>AJAX Request/Response</h3>
<p></p>
<input id="input" type="button" value="Call AJAX" />
<p></p>
<div id="response"></div>
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?