bug^君 2014-03-28 23:27 采纳率: 25%
浏览 40

AJAX加载不起作用?

This is my code and im trying to load html from demo_test.txt into it, but it won't work no matter what I try, ive tried changing it from txt to html as well and it still won't load. The other ajax command such as appending the text works correctly its the load line which won't work?

<head>
    <title>Flask AJAX Demo</title>
    <!--<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style2.css') }}"> -->

    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <script type=text/javascript
      src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script type=text/javascript>
      var $SCRIPT_ROOT = {{ request.script_root|tojson|safe }};
    </script>        

    <script type=text/javascript>
  $(function() {
$("#submitBtn").click(function() {
    $('#div1').load('dem_test.txt');
     $.ajax({
        type: "GET",
        url: $SCRIPT_ROOT + "/echo/",
        contentType: "application/json; charset=utf-8",
        data: { echoValue: $('input[name="echoText"]').val() },
        success: function(data) {
            $('#echoResult').append("<br>");
            $('#echoResult').append(data.value);

        }
    });     
});
  });

</script>
<strong>Enter a value to echo back:</strong>
<input type='text' size='10' id='echoText' name='echoText'>
<button type='button' id='submitBtn' name='submitBtn'>Submit via AJAX</button><br /><br />
<strong><div id='echoResult'></div></strong>
<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>

This is what is in the demo_test.txt, this wont work if it is html either

<h2>jQuery and AJAX is FUN!!!</h2>
<p id="p1">This is some text in a paragraph.</p>

EDIT:

I changed my load to this:

$('#div1').load('dem_test.txt', function(response, status, xhr) {
if ( status == "error" ) {
    alert("Error occured: " + xhr.status + " " + xhr.statusText );
} else {
    alert("Text loaded!");
}

});

But I get Error occured: 404 error , but the dem_test.txt file is in the same folder as the html file?

EDIT 2

I have tried this exact example as well found here: .load() method in jquery give me 404 - Not Found error BUT it won't work like his didnt either, what should the file location be?

  • 写回答

1条回答 默认 最新

  • weixin_33749242 2014-03-28 23:40
    关注

    It is better to use jquery load in this way;

    $('#div1').load('dem_test.txt', function(response, status, xhr) {
        if ( status == "error" ) {
            alert("Error occured: " + xhr.status + " " + xhr.statusText );
        } else {
            alert("Text loaded!");
        }
    
    });
    

    You can see alert if file is loaded

    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法