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);
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来