weixin_33674437 2017-07-07 08:03 采纳率: 0%
浏览 80

用ajax回显不起作用

I was following an tutorial, and i finish with this code:

<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
    $(function($){
      $("#compila").click(function(){
          var code = $(#codigo).html();

          $.post("myajax.php",{code:code},function(return){
            $(#result).html("<b>print </b>"+return);
          });
      });
    });

</script>

<?php
      $code = $_POST["code"];
      echo $code;

 ?>

but this code doesn't work. I was using a text area, and i'm trying to send a string of the text area to a

tag, and print with echo of the php. How i can fix this code?

  • 写回答

2条回答 默认 最新

  • weixin_33699914 2017-07-07 08:05
    关注

    You're missing quotes around some of your identifiers:

    var code = $(#codigo).html();
    

    should be

    var code = $("#codigo").html();
    

    and

    $(#result).html("<b>print </b>"+return);
    

    should be

    $("#result").html("<b>print </b>"+return);
    

    If you use the developer console in your browser (<kbd>F12</kbd> and go to the console tab) it should then tell you about these errors and give you the line number of where the errors occur.

    As userr1T77 mentioned in his answer, you need to change the variable name from return to something else. return is a reserved keyword so can't be used as a variable, so your code should be something similar to

    $.post("myajax.php",{code:code},function(return_data){
        $("#result").html("<b>print </b>"+return_data);
    });
    
    评论

报告相同问题?