[英]Load PHP File Using Ajax (Without JQUery)
I have the following Ajax code in an html file:我在 html 文件中有以下 Ajax 代码:
<script type="text/javascript" src="jquery-3.5.0.min.js">
$.ajax({
method: "GET",
url:"nba2019_namelist.php",
success:function(res) {
$("#playerNames").html(res)
}
})
</script>
This is supposed to load a php file (which really just creates a list from a csv), but is not working.这应该加载 php 文件(实际上只是从 csv 创建一个列表),但不起作用。 I am using Apache to make php function, and when I go to http:/localhost/nba2019_namelist.php, my list is present, so I am fairly certain that the php file isn't the issue.
I am using Apache to make php function, and when I go to http:/localhost/nba2019_namelist.php, my list is present, so I am fairly certain that the php file isn't the issue. The ajax code is meant to replace the following html list:
ajax 代码旨在替换以下 html 列表:
<div>
<ul id="playerNames">
<li><b>Harden</b></li>
<li><b>Giannis</b></li>
<li><b>Lebron</b></li>
<li><b>Booker</b></li>
<li><b>Lavine</b></li>
<li><b>Westbrook</b></li>
<li><b>Jokic</b></li>
</ul>
</div>
But the only output when I load the page are the same names that are typed in here, not the ones created by my php file.但是,当我加载页面时,唯一的 output 与此处输入的名称相同,而不是由我的 php 文件创建的名称。 What am I doing wrong here?
我在这里做错了什么? Do I need to specify in the Apache httpd.conf which php file I want to load?
我是否需要在 Apache httpd.conf 中指定要加载哪个 php 文件? I don't really know any Ajax, but based on what I have seen on forums, it should work.
我真的不知道任何 Ajax,但根据我在论坛上看到的,它应该可以工作。 What am I doing wrong here, and what should I do next to fix this issue?
我在这里做错了什么,接下来我应该怎么做才能解决这个问题?
If JQuery is the only solution, just let me know, I would just rather not learn something new at the moment, unless it is completely necessary.如果 JQuery 是唯一的解决方案,请告诉我,我宁愿暂时不学习新东西,除非完全有必要。
I was able to fix my own problem.我能够解决我自己的问题。 I followed a tutorial online that suggested changing my vanilla
<script>
tag that contained my ajax code to instead read <script type="text/javascript" src="jquery.min.js">
.我遵循了一个在线教程,该教程建议将包含我的 ajax 代码的原版
<script>
标签改为阅读<script type="text/javascript" src="jquery.min.js">
。 This made my code not function properly.这使我的代码不能正确地使用 function。
To solve this, I changed my script tag back to the vanilla <script>
, and instead put the following at the bottom of my <head>
: <script type="text/javascript" src="jquery.min.js"></script>
为了解决这个问题,我将脚本标签改回原版
<script>
,而是将以下内容放在<head>
的底部: <script type="text/javascript" src="jquery.min.js"></script>
This allowed my php code to be grabbed from ajax as expected.这使得我的 php 代码可以按预期从 ajax 中获取。
Try this by using fetch:使用 fetch 试试这个:
fetch('nba2019_namelist.php')
.then(response=>response.Text())
.then(data => {
document.getElementById("playerNames").innerHTML = data;
});
I hope it helps我希望它有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.