Ajax:尝试调用PHP脚本时“未找到元素” - javascript

我试图在我的Ubuntu 14.04 LTS系统上使JavaScript和PHP一起工作。为此,我创建了以下三个文件:

index.html

<html>
    <head>
        <script type="text/javascript" src="testJS.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    </head>
    <body>
        <button onClick="testFunction();">Click Me</button>
        <div id="testDiv"></div>
    </body>
</html>

testJS.js

function testFunction() {
    $.ajax({
        url: "testPHP.php",
        success: function(result) {
            $("#testDiv").html(result);
        }
    });
}

testPHP.php

<?php
echo("<h1>This is coming from PHP.</h1>");
?>

以上所有文件都存储在我的桌面上的单个文件中。在Firefox中打开index.html并单击按钮后,网页没有任何反应,并且Firefox控制台仅显示以下JavaScript错误:

no element found          testPHP.php:4:1
no element found          index.html:4:1

我已经阅读了本网站上有关此问题的其他主题。将header('Content-Type: text/plain'); exit();添加到我的PHP文件中没有任何效果。

任何帮助,将不胜感激。谢谢。

更新:我已经安装了Apache2,通过查看Apache的默认页面来确认它可以正常工作,并将所有三个文件都移至/ var / www / html。我遇到了同样的问题。

参考方案

尝试这个:

function testFunction() {
 
  $.ajax({
    url: "<?php echo base_url();?>testPHP.php",
    type:"post",
    success: function(result) {
      $("#testDiv").html(result);
      alert(1)
    }
  });
}

<html>

<head>
 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>

<body>
  <button onClick="testFunction();">Click Me</button>
  <div id="testDiv"></div>
</body>

</html>

ajax:从json数组中提取json对象 - javascript

JSON(来自api的内部数组):[{"id":"001", "name":"john", "age":"40"}, {"id":"002", "name":"jane&…

Telerik单选按钮所需的字段验证器 - javascript

如何设置Telerik单选按钮所需的字段验证器?我想在按钮单击“ BtnSave”上设置必填字段验证器吗?请帮忙!<telerik:RadButton ID="radio_male" runat="server" ToggleType="Radio" AutoPostBack="fa…

表单不提交或按钮提交不起作用 - javascript

据我所知,此代码必须有效,但是我编码时却无效问题在于该表单未提交。我该如何解决?选中时,我什至没有得到复选框的值。<table id="example" class="display" cellspacing="0" width="100%"> <thead&g…

AJAX-如何在弹出菜单中运行脚本 - javascript

我有一个弹出菜单,此菜单打开并显示文本。我想要的是能够将控制器附加到弹出菜单。例如:我希望显示一个编辑配置文件弹出窗口,其中包含编辑该配置文件的所有必要输入。userprofile_view.php: <div class="upload"> <?php $data = array('id' =>…

和字符中断通过PHP接收JSON - javascript

我有一个包含更多对象的数组。如果一个对象包含&字符,则php不会接收&之后的每个对象。可能是什么问题?这就是阿贾克斯xmlhttp.open("POST", "get.php"); xmlhttp.setRequestHeader("Content-Type", "application/…