jQuery中的.load()方法给我404-找不到错误 [英] .load() method in jquery give me 404 - Not Found error

查看:471
本文介绍了jQuery中的.load()方法给我404-找不到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的页面,我需要将.html文件加载到DIV中,但是我总是有404 NOT FOUND

I have a simple page that I need to load .html file to a DIV ,but I always have 404 NOT FOUND

我的JSP文件的同一文件夹中有一个名为demo_test.txt的文件,我需要将此文件加载到ID为"div1"的div中.

I have a file called demo_test.txt in the same folder of my JSP file,I need to load this file to the div with ID="div1"

这是代码:

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
   // $("#div1").load("demo_test.txt");
   $("#div1").load("demo_test.txt", function(responseText, statusText, xhr)
        {
                if(statusText == "success")
                        alert("Successfully loaded the content!");
                if(statusText == "error")
                        alert("An error occurred: " + xhr.status + " - " + xhr.statusText);
        });

  });

});

</script>
</head>
<body>

<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
<button>Get External Content</button>

</body>
</html>

推荐答案

像克劳迪奥·雷迪(Claudio Redi)所说的...

Like Claudio Redi said...

您正在尝试加载相对于您所在路径的文件.尝试对文件路径进行硬编码,然后在浏览器中查看它,然后将其加载.

you're trying to load a file relative to the path you are at. Try hard coding the file path and then viewing it your browser and then it should load.

因此您的行将如下所示(如果文件位于WEBROOT/MY_APP/Files中)

so your line will be like this (if file is located in WEBROOT /MY_APP/Files)

$("#div1").load("/MY_APP/Files/demo_test.txt", function(responseText, statusText, xhr)

或完整路径

$("#div1").load("http://MyWebsite.com/MY_APP/Files/demo_test.txt", function(responseText, statusText, xhr)

这篇关于jQuery中的.load()方法给我404-找不到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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