Neeraj Verma
Neeraj Verma

Reputation: 263

jquery ajax method is not Loading Text File

I am learning jquery ajax() from w3c school http://www.w3schools.com/jquery/ajax_ajax.asp

I have one html file , 1 text file("demo_test.txt") in same folder. on button click . html should load text file :

here is code :

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.ajax({url:"demo_test.txt",success:function(result){
      $("#div1").html(result);
    }});
  });
});
</script>
</head>
<body>

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

</body>
</html>

Text file only have data "this is text file".

But some How it is not working . Other similar post dont have this scenario.. is there any problem placing text file physically ? please suggest

Upvotes: 1

Views: 2342

Answers (2)

Neeraj Verma
Neeraj Verma

Reputation: 263

I finally seems to Get it working . Here is working Script

$("button").click(function(){
    $.ajax({url:"http://localhost/demo_test.txt",success:function(result){
      $("#div1").html(result);
    }});
  });

Workaround : put the html file and text file on local server (IIS) New Site .

Upvotes: 1

Pragnesh Khalas
Pragnesh Khalas

Reputation: 2898

You’re displaying the txt file in the browser but when you click on button at the time page was postback then your text file content not displayed on the page so you need to just restrict to postback the page. So you just change javascript as below

<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        $("button").click(function () {
            $.ajax({ url: "demo_test.txt",
                success: function (result) {
                    $("#div1").html(result);
                },
                error: function (abc) {
                    alert(abc.statusText);
                },
                cache:false
            });
            return false;
        });
    });
</script>

Upvotes: 0

Related Questions