Friday, December 7, 2012

Reading Text file in Jquery Ajax

First create a dummy text file in your project  named as  text.txt , Now write some text content in that file.
You can read that file through JQuery Ajax request.
Alert will show the text content of written file.

 Code:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script>
getTxt = function (){
$.ajax({
url:'test.txt',            // Text file name
success: function (data){
   alert(data);    // or do some other data processing
}
});
}
$(function () {
 getTxt();  // calling ajax function on page load
 });
</script>


======================Update on getting AJAX reponse

AJAX is asyn it doesnt wait for send response.

function getddlData() {
    var ajaxResponse;
    $.ajax({
        type: "POST",
        url: 'TextEditor.aspx/GetBookMarkData',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        cache: false,
        // Text file name
        success: function (response) {
            //    //alert(data.d);    // or do some other data processing
            //   //return data.d;
            ajaxResponse = response;
        }
    });
    return ajaxResponse;

Now you can use it like :

 var response = getddlData();
        alert(response.d); 

No comments:

Post a Comment