每当单击按钮时,目标文件都会获取"demo.txt“(签入网络),但是文件内容没有显示,有什么问题吗?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#button").click(function(){
$.ajax({url: "http://localhost/suman_php/JQ_AJAX/demo.txt",
type:"GET",
success: function(data,status){
alert(status);
$("#div1").html(data);
}});
});
});
</script>
</head>
<body>
<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
<button id="button">Click to get Content</button>
</body>
</html>发布于 2020-05-04 08:23:52
我认为您正在尝试在您的Div中显示文本文件内容,同时单击该按钮。您不需要GET方法,除非您试图从链接中获取特定的数据,以便在后端的其他地方使用它,例如在PHP中使用它。试试这个:
$.ajax({
url: "http://localhost/suman_php/JQ_AJAX/demo.txt",
async: false, //to wait for text to load
dataType: "text", // jQuery will return the text data from the file
success: function( data, status ) {
alert(status);
$("#div1").html(data);
}
});发布于 2020-05-04 06:51:03
我建议你用“方法”代替“类型”
( b)定义一个"error“函数,如下所定义:https://api.jquery.com/jquery.ajax/,它可能为您提供请求失败的一些信息。
发布于 2020-05-04 09:36:11
$(document).ready(function(){
$("button").click(function(){
$.get("http://localhost/suman_php/JQ_AJAX/demo.txt", function(data, status){
alert(status);
$("#show").html(data);
});
});
});在这段代码中应该修改什么来查看文件内容?
https://stackoverflow.com/questions/61586568
复制相似问题