获取 HTML 中的 console.log

huangapple go评论78阅读模式
英文:

Get console.log in HTML

问题

以下是翻译好的部分:

<!DOCTYPE html>
<html>
<title>Online 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>
英文:

I have written an online editor code which compiles java codes, the problem is that I get the output from the compiler as console.log()

Output is : {Warnings: null, Errors: null, Result: &quot;Hello World!↵&quot;, Stats: &quot;Compilation time: 0.72 sec, absolute running time:…mory peak: 35 Mb, absolute service time: 1,09 sec&quot;, Files: null,&#160;…}

I want the output to be shown in the HTML itself, any solution ??

So far my code is:

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

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

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

	&lt;/script&gt;
    &lt;textarea id=&quot;code&quot; rows=&quot;20&quot; cols=&quot;100&quot;&gt;

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

    System.out.println(&quot;Hello World!&quot;);
  }
}  
    
    &lt;/textarea&gt;&lt;br&gt;
	&lt;button id=&quot;run&quot;&gt;Run&lt;/button&gt;
	&lt;br&gt;
	

&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 1

你可以在HTML中放置一个带有ID的div,然后使用$("#result_id").html(data)$("#result_id").text(data)

<!DOCTYPE html>
<html>
<title>在线编译</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>
英文:

You could place a div with ID in html and then use $(&quot;#result_id&quot;).html(data)&#39; or &#39;$(&quot;#result_id&quot;).text(data)

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

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

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

    &lt;/script&gt;
    &lt;textarea id=&quot;code&quot; rows=&quot;20&quot; cols=&quot;100&quot;&gt;

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

    System.out.println(&quot;Hello World!&quot;);
  }
}  
    
    &lt;/textarea&gt;&lt;br&gt;
    &lt;button id=&quot;run&quot;&gt;Run&lt;/button&gt;
    &lt;br&gt;

    &lt;div id=&quot;result&quot;&gt;&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2020年10月19日 19:24:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64426329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定