dq804806 2017-06-14 18:34
浏览 89
已采纳

Ajax新手学习(golang jquery)

Ok, so I have thoroughly scoured stackoverflow looking for solutions that can make my code work, and I believe I am close, but I cant tell exactly why my code isnt working.

So, I am trying to build a dynamic content page and have an ajax request sent from click onto my notes to allow them to be expanded and viewed, and edited.

Heres the script I am trying to use:

<script>
$('.notes').on('click',function (e){
   alert("ok");
   $.ajax({
    type:'GET',
    url :'localhost:8080/editnote',
    dataType: 'html',
    success: function(data) {
        console.log('success',data);
        $('#interactive').html(data);

    },
    error: function(jqXHR,textStatus,errorThrown ){
       alert('Exception:'+errorThrown );
    }
});
 e.preventDefault();
});
</script>

Now, here is the golang snippet I am trying to get it to interact with. It doesn't seem to be connecting whatsoever to my server since the handlefunc never registers the troubleshooting fmt println.

handlefunc snippet:

func test(w http.ResponseWriter, r *http.Request) {
    fmt.Println("code got here")
    s := `Here is some text from test`
    fmt.Fprintln(w, s)
}

and here is the main() snippet:

func main() {
    http.HandleFunc("/editnote", test)
    http.ListenAndServe(":8080", nil)
}

Any insight here would be highly valued. Thank you so much for reviewing this question.

edit: I forgot to mention that the alert on the script successfully fires, so the on click script is working, just not the ajax.

edit2: via developer console:

Uncaught TypeError: $.ajax is not a function
    at HTMLDivElement.<anonymous> ((index):153)
    at HTMLDivElement.dispatch (jquery-3.2.1.slim.min.js:3)
    at HTMLDivElement.q.handle (jquery-3.2.1.slim.min.js:3)

Update: it appears that the issue here after i did some digging about the console was that i was using a slim version of jquery 3.2.1. After switching to a minified non-slim version, i am getting server responses to the query! I still have loads of work to do on this app, but this was a very valued solution! Thanks very much to everyone who helped me diagnose this problem!

  • 写回答

1条回答 默认 最新

  • dqwr32867 2017-06-14 19:08
    关注

    It seems jQuery slim build, doesn't support ajax function. So include jquery-3.2.1.min.js try, it will work.

    SO post: Read about normal vs slim build.

    Can you try this jquery ajax call?

    $.ajax({
      type: 'GET', // default is GET, so you can exclude if you want
      url : '/editnote', // don't hard the host address
      dataType: 'html',
      success: function(data) {
         console.log(data);
      },
      error: function(e){
         console.log(e);
      }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。