javascript运行xmlhttp php脚本404未找到 [英] javascript run xmlhttp php script 404 not found

查看:40
本文介绍了javascript运行xmlhttp php脚本404未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使 .js 文件在同一文件夹中运行 .php 文件.我正在使用 XMLHttpRequest.目前,.js 脚本与节点服务器一起运行.

I'm trying to make a .js file run a .php file in the same folder. I'm using XMLHttpRequest. Currently, the .js script is running with a node server.

我尝试了各种方法来获取 .php 文件.

I've tried a variety of things for the url to get the .php file.

  • my_file.php 导致 "404 (Not Found)"
  • //InsertAbsolutePathHere/tests/my_file.php 导致 net::ERR_NAME_NOT_RESOLVED
  • file:///InsertAbsolutePathHere/tests/my_file.php 导致 跨源请求仅支持协议方案:http、data、chrome、chrome-extension、https.
  • my_file.php results in a "404 (Not Found)"
  • //InsertAbsolutePathHere/tests/my_file.php results in a net::ERR_NAME_NOT_RESOLVED
  • file:///InsertAbsolutePathHere/tests/my_file.php results in a Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

如果我使用第一个选项(相对路径),我会注意到最终 url 以localhost:3000/tests"开头.当我使用绝对路径时,我可以在 Chrome 中很好地查看文件,但是 localhost:3000/tests 版本向我显示Cannot GET"消息.但是,如果我为 index.html 或任何其他 html 页面使用相对路径,我的代码似乎可以很好地找到该文件.仅当它是 php 文件的相对路径时才会抛出 404.

If I use the first option (relative path), I notice that the final url starts with "localhost:3000/tests". I can view the file in Chrome just fine when I use the absolute path, but the localhost:3000/tests version shows me a "Cannot GET" message. However, if I use a relative path for index.html, or any other html page for that matter, my code seems to find the file just fine. It only throws a 404 when it's a relative path to a php file.

这是我的文件组织:

  • 文件
    • js
      • index.js(我想从中调用我的 php 的文件)
      • index.html
      • my_file.php

      我感觉我的相对路径不起作用,因为我正在运行一个节点服务器,并且该服务器不能很好地处理 php 文件.有人可以确认吗?

      I have a feeling my relative path isn't working because I'm running a node server, and that the server isn't playing nice with php files. Can someone confirm this?

      如何加载我的 php 脚本?另外,两个绝对路径选项之间有什么区别?为什么它们返回不同的错误消息?

      How do I get my php script to load? Also, what's the difference between the two absolute path options? Why are they returning different error messages?

      提前致谢!

      这是我的代码(其中一部分取自另一个帖子)

      Here's my code (part of which was taken from another post)

      function drawError() {
          var container = document.getElementById('output');
          console.log('Bummer: there was an error!');
      }
      // handles the response, adds the html
      function drawOutput(responseText) {
          console.log(responseText);
      }
      function getRequest(url, success, error) {
          var req = false;
          try{
              // most browsers
              req = new XMLHttpRequest();
          } catch (e){
          console.log("XML request failed.");
              return false;
          }
          if (!req) return false;
          if (typeof success != 'function') success = function () {};
          if (typeof error!= 'function') error = function () {};
          req.onreadystatechange = function(){
              if(req.readyState == 4) {
                  return req.status === 200 ? success(req.responseText) : error(req.status);
              }
          }
          req.open("GET", url, true);
          req.send(null);
          return req;
      }
      var urlString1 = 'my_file.php?' + encodeURIComponent('game_id') + '=0&' + encodeURIComponent('client_timestamp') + '=10';
      //var urlString2 = '//Users/Emma/Documents/tests/my_file.php';
      //var urlString3 = 'file:///Users/Emma/Documents/tests/my_file.php';
      console.log(urlString);
      var urlRequest = getRequest(
          urlString, // URL for the PHP file 
          drawOutput,  // handle successful request 
          drawError    // handle error
      );
      console.log(urlRequest);
      

      推荐答案

      没关系,我是个白痴.我有两个非常相似的文件夹,我只是将文件放在错误的文件夹中.它现在正在工作.感谢所有试图提供帮助的人!

      Nevermind, I'm an idiot. I had two very similar folders and I simply put my file in the wrong folder. It's working now. Thanks to everyone who tried to help!

      这篇关于javascript运行xmlhttp php脚本404未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆