Hi I am a complete noob in this. I am trying to call a simple php page from js script
$.ajax({
url: "././mail/testphp.php",
type: "POST",
dataType: "html",
success: function() {
// Success message
alert("success");
},
error: function(xhr,textStatus,err) {
alert("readyState: " + xhr.readyState + "
" +
"responseText: " + xhr.responseText + "
" +
"status: " + xhr.status + "
" +
"text status: " + textStatus + "
" +
"error: " + err);
},
})
This is the testphp:
<?php
echo 'Hello';
?>
The alert I am getting is "NetworkError: Failed to execute send on XMLHttpRequest failed to load file dir/testphp" (the directory is correct).
Both js and php are on my local hard drive. Do I need to put it on a server for it to work or something?
edit: here's the error I am getting (note: the directory IS correct)
Are you loading all files (html and js) from file? (e.i. file://....). If you are loading them through a local webserver, you may face a cross site scripting block from your browser. You can easily find such block if you use google chrome and open the developer tools.
You then can start chrome with a special flag to circumvent this problem (see Chrome Allow File Access From File. However, I reccommend to install a local webserver you can use for testing.
Correct your ajax url :
url: "././mail/testphp.php",
The reason you are getting NetworkError is because your url is not mapping
EDIT
I think this might work
url: "testphp.php",
UPDATED
I think you should add the data to be post like :
url: "testphp.php",
data: "", // data to be post on testphp.php
type: "POST",
Use this:
url: "../../mail/testphp.php",
instead of this:
url: "././mail/testphp.php",
This would work.
url: '../mail/testphp.php',
. => one dot mean current directory .. => two dots means go up one directory.
either use this url: "../../mail/testphp.php"
in order to go up for 2 levels
or use url: "../mail/testphp.php"
to go up by 1 level
try any one.