好的,所以我已经彻底搜索了堆栈溢出,寻找能够使我的代码工作的解决方案,我相信我已经接近了,但是我无法确切地知道为什么我的代码不能工作。
因此,我正在尝试构建一个动态内容页面,并通过单击将ajax请求发送到我的笔记上,以便展开、查看和编辑它们。
下面是我试图使用的脚本:
<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>现在,这是我试图让它与之交互的金刚片段。它似乎没有连接到我的服务器,因为handlefunc从来没有注册故障排除fmt。
handlefunc片段:
func test(w http.ResponseWriter, r *http.Request) {
fmt.Println("code got here")
s := `Here is some text from test`
fmt.Fprintln(w, s)
}下面是main()代码段:
func main() {
http.HandleFunc("/editnote", test)
http.ListenAndServe(":8080", nil)
}这里的任何洞察力都会被高度重视。非常感谢你回顾这个问题。
编辑:我忘了提到脚本上的警报会成功触发,所以on脚本是有效的,而不是ajax。
edit2:通过开发者控制台:
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)更新:在我对控制台做了一些调查之后,这里的问题似乎是我使用的是jQuery3.2.1的苗条版本。切换到一个小型化的非苗条版本后,我将得到服务器对查询的响应!我仍然有很多工作要做的这个应用,但这是一个非常宝贵的解决方案!非常感谢每一个帮助我诊断这个问题的人!
发布于 2017-06-14 19:08:13
看起来jQuery苗条的身材,不支持ajax函数。因此,包括
jquery-3.2.1.min.js尝试,它将工作。
所以发帖:阅读关于正常的和苗条的身材的文章。
您能试试这个jquery调用吗?
$.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);
}
});https://stackoverflow.com/questions/44552209
复制相似问题