0

I tried to create a simple AJAX call to php script.

request.js

var xhr = new XMLHttpRequest();
xhr.open('GET', './assets/script/api/sample.php');

xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
        console.log(xhr.responseText);
    }
}

xhr.send();

sample.php

<?php echo "Hello World"; ?>

Error:

Failed to load resource: the server responded with a status of 404 (Not Found) (localhost:7456/assets/script/api/sample.php)

Krissy
  • 13
  • 4
  • is `/assets/script/api/sample.php` the correct path to sample.php? – Bravo Nov 08 '18 at 03:17
  • Hi @Krissy please check sample.php path and file name – Saurabh Gujarani Nov 08 '18 at 03:18
  • it is a correct path. – Krissy Nov 08 '18 at 03:20
  • 1
    what do you get if you put `http://localhost:7456/assets/script/api/sample.php` in your browser address – Bravo Nov 08 '18 at 03:21
  • Could you have a rewrite rule on the server that's changing the path? Check the access log. – Barmar Nov 08 '18 at 03:24
  • I don't think I've ever seen that error come from an AJAX request. I have seen it come from ` – Phil Nov 08 '18 at 04:09
  • this .js file is for the game project I am making from cocos2d-js. I tried to open the sample.php to the browser and returns 404 error. my possible guess is that my php was placed somewhere on the server after building. – Krissy Nov 08 '18 at 05:39

1 Answers1

0

Recently fixed this problem. The php file should be placed on the build folder.

Krissy
  • 13
  • 4