簡體   English   中英

在 HTML 中獲取 console.log

[英]Get console.log in HTML

我已經編寫了一個編譯 java 代碼的在線編輯器代碼,問題是我從編譯器獲得輸出為console.log()

輸出是: {Warnings: null, Errors: null, Result: "Hello World!↵", Stats: "Compilation time: 0.72 sec, absolute running time:…mory peak: 35 Mb, absolute service time: 1,09 sec", Files: null, …}

我希望輸出顯示在 HTML 本身中,有什么解決方案嗎??

到目前為止,我的代碼是:

<!DOCTYPE html>
<html><title> Onile Compile</title
<body>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
    
    $(document).ready(function(){
        $("button").click(function(){
            var ultimate_test_result = 24;

            var to_compile = {
                "LanguageChoice": "4",
                "Program": $("#code").val(),
                "Input": "",
                "CompilerArgs" : ""
            };

            $.ajax ({
                    url: "https://rextester.com/rundotnet/api",
                    type: "POST",
                    data: to_compile
                }).done(function(data) {
                    //alert(JSON.stringify(data));
                    console.log(data); 
                }).fail(function(data, err) {
                    alert("fail " + JSON.stringify(data) + " " + JSON.stringify(err));
                });
        });
        
    });

    </script>
    <textarea id="code" rows="20" cols="100">

  class Rextester {
  public static void main(String[] args) {

    System.out.println("Hello World!");
  }
}  
    
    </textarea><br>
    <button id="run">Run</button>
    <br>
    

</body>
</html>

您可以在 html 中放置一個帶有 ID 的 div,然后使用$("#result_id").html(data)' or '$("#result_id").text(data)

<!DOCTYPE html>
<html><title> Onile Compile</title
<body>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
    
    $(document).ready(function(){
        $("button").click(function(){
            var ultimate_test_result = 24;

            var to_compile = {
                "LanguageChoice": "4",
                "Program": $("#code").val(),
                "Input": "",
                "CompilerArgs" : ""
            };

            $.ajax ({
                    url: "https://rextester.com/rundotnet/api",
                    type: "POST",
                    data: to_compile
                }).done(function(data) {
                    //alert(JSON.stringify(data));
                    console.log(data); 
                    $("#result").text(data);
                }).fail(function(data, err) {
                    alert("fail " + JSON.stringify(data) + " " + JSON.stringify(err));
                });
        });
        
    });

    </script>
    <textarea id="code" rows="20" cols="100">

  class Rextester {
  public static void main(String[] args) {

    System.out.println("Hello World!");
  }
}  
    
    </textarea><br>
    <button id="run">Run</button>
    <br>

    <div id="result"></div>

</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM