-1

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 + "\n" +
                      "responseText: " + xhr.responseText + "\n" +
                      "status: " + xhr.status + "\n" +
                      "text status: " + textStatus + "\n" +
                      "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) enter image description here

9
  • try to using ../ instead of ./ Commented May 6, 2015 at 4:49
  • Are you sure its ././mail/testphp.php and not ../../mail/testphp.php? Commented May 6, 2015 at 4:50
  • @Sourabh- yeah, this code is pretty much from bootstrap template including all their files/folders (I've only changed filename b/c theirs wasn't working either with same error) Commented May 6, 2015 at 5:02
  • @MichaelNaumov. In that case can you show your directory structure? Commented May 6, 2015 at 5:04
  • Try writing a regular form that submits via POST to testphp, what does it do? Commented May 6, 2015 at 5:09

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

yeah they are all on hard drive, I am just clicking the index.html in windows explorer to preview.
oh my god thanks for suggestion! I was about to just abandon my attempt at using php. I installed XAMPP and run my webpage through that, and success!
Good to hear. I can advice strongly to use google chrome developer tools (which is part of the browser) to debug and check if you have errors in your javascript etc. You can also use firefox inspector for that.
1

. => one dot mean current directory .. => two dots means go up one directory.

Comments

0

Use this:

url: "../../mail/testphp.php",

instead of this:

url: "././mail/testphp.php",

4 Comments

no this gives completely incorrect directory in the error log
so the dir is no Problem?
but you dont Need the ././
nope sorry. I tried everything, even moving the testphp into root folder and removing the slash completely. Same issue. The error log itself prints out the correct directory which is why I suspect it's something else. Perhaps I need to run it from a server instead of local machine? I got the code from web template without any modifications except trying to replace the php file (since the one they provided initially gave me the same error)
0

This would work.

url: '../mail/testphp.php',

3 Comments

hmm that may have worked, except its not showing the success alert but refreshes the page instead
Do not print values in your testphp.php, instead try database operation like insert, delete so that u will get to know whether this call is success or not.
ah nevermind, I accidently deleted a comma after url when I tried your solution. That way instead of giving an error it was refreshing a page. Still doesn't work sorry. :(
0

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",

2 Comments

tried it, also tried moving testphp.php file into the same folder with js. The error log shows correct URL. :(
what is the full url of the testphp.php file and your current file from which you are calling
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.