Javascript read html from url into string

后端 未结 3 1094
梦毁少年i
梦毁少年i 2020-12-14 08:05

I\'m currently building a site that should be able to function as a ftp sort of browser. Basically what I have is a ftp server with some images on it.

What I can\'t

相关标签:
3条回答
  • 2020-12-14 08:41

    Your answer is Ajax. It can POST and GET data from an URL, just like browsing a website, and it will return the HTML as a string.

    If you plan on using jQuery (real handy), it is easy to use Ajax. Like this example (does not work without the library):

    $.ajax({
        url : "/mysite/file.html",
        success : function(result){
            alert(result);
        }
    });
    

    If you want to use default Javascript, take a look at http://www.w3schools.com/ajax/default.asp

    var xmlhttp;
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "ajax_info.txt", true);
    xmlhttp.send();
    
    0 讨论(0)
  • 2020-12-14 08:43

    There's not much to add to what Niels and rich.okelly have said. AJAX is your way to go.

    Keep in mind though, that cross-domain restrictions will prohibit you to access data that is not in the same domain. You'll find a possible workaround here.

    0 讨论(0)
  • 2020-12-14 08:55

    IN Javascript to get data without using alert() :

        $.ajax({
        url : "/mysite/file.html",
        async:false,            //this is the trick
        success : function(result){
                     //does any action
                   } 
        });
    
    0 讨论(0)
提交回复
热议问题