简体   繁体   English

Ajax呼叫似乎无法识别我的文件

[英]Ajax call does not seem to recognize my file

I have a very simple HTML file which has one button. 我有一个非常简单的HTML文件,其中包含一个按钮。 When this button is clicked the function loadDoc() runs in the javascript file (ajax.js). 单击此按钮后,函数loadDoc()将在javascript文件(ajax.js)中运行。 The div with ID ajax_text gets changed into 'clicked'. ID为ajax_text的div更改为“ clicked”。 So far so good. 到现在为止还挺好。

Now I am trying to make ajax call to a php document. 现在,我正在尝试对php文档进行ajax调用。 The php document should echo "Hello World!". php文档应回显“ Hello World!”。 I am trying to show this message by using an alert (alert(msg)). 我正在尝试通过使用警报(alert(msg))显示此消息。 The php document is located in the same folder as my HTML document. php文档与HTML文档位于同一文件夹中。

What am I doing wrong in my ajax call?? 我的ajax通话中我在做什么错?

I already tried multiple urls such as: - ' http://localhost/ajax_info.php ' - the complete path to the file - 'localhost/ajax_info.php' 我已经尝试了多个URL,例如:-'http ://localhost/ajax_info.php'-文件的完整路径-'localhost / ajax_info.php'

etc. etc. 等等等

HTML: HTML:

<!DOCTYPE html>
<html>
  <head>
  </head>

  <body>
    <div id="name">John</div>
    <div id="demo">
      <h2>Header 2</h2>
      <button type="button" onclick="loadDoc()">Change Content</button>
    </div>
    <script src="jquery-3.2.1.js"></script>
    <script src="ajax.js"></script>
  </body>
</html>

JavaScript: JavaScript的:

function loadDoc() {

document.getElementById('name').innerHTML = "Steven";

    $.ajax({
        type: 'GET',
        url: 'ajax_info.php',
        success: function(msg){
            alert(msg);
        }
    });
 }

PHP: PHP:

<?php  echo "Hello World"; ?>

It looks as if you call your html page as file from your file-explorer, but not - as you should - via your web-server. 似乎您是从文件资源管理器中将html页面作为文件调用的,而不是通过Web服务器(如您应有的那样)调用。
If your address in the browser has file://somefolders/myhtml.html your php script (well, actually the server) will say "hello, that's not where I am!" 如果您在浏览器中的地址具有file://somefolders/myhtml.html您的php脚本(实际上是服务器)会说“你好,那不是我的位置!” -> It throws "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access." ->引发“请求的资源上不存在'Access-Control-Allow-Origin'标头。因此,不允许访问源'null'。”

Now you can tell your server "Nah, don't bother, I'll just allow everyone!" 现在您可以告诉您的服务器“不,不要打扰,我只允许所有人!”
This is what you do when setting the header('Access-Control-Allow-Origin: *'); 这是设置header('Access-Control-Allow-Origin: *');

But your initial problem was that the html-file wasn't called at the same origin, namely your (local) server. 但是您最初的问题是html文件不是在同一来源(即您的(本地)服务器)调用的。

So if you type in your browser's address bar http://localhost/ajax.html and don't just double click the file in file-explorer it should work, because now they both (html and php) live in the same environment. 因此,如果您在浏览器的地址栏中输入http://localhost/ajax.html不仅仅是在file-explorer中双击该文件,它应该可以工作,因为现在它们(html和php)都位于同一环境中。

try to write the javascript code inside the page that has html. 尝试在具有html的页面内编写javascript代码。 it should works . 它应该可以工作。

<!DOCTYPE html>
<html>
<head>

</head>

<body>


<div id="name">John</div>

<div id="demo">
  <h2>Header 2</h2>
  <button type="button" onclick="loadDoc()">Change Content</button>
</div>


<script src="jquery-3.2.1.js"></script>
<script>
  function loadDoc() {
  document.getElementById('name').innerHTML = "Steven";

    $.ajax({
        type: 'GET',
        url: 'ajax_info.php',
        success: function(msg){
            alert(msg);
        }
    });
  }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM