好的,所以我已经彻底搜索了 stackoverflow 以寻找可以使我的代码正常工作的解决方案,我相信我已经接近了,但我不能确切地说出我的代码为什么不工作。
所以,我正在尝试构建一个动态内容页面,并通过单击将 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>
现在,这是我试图让它与之交互的 golang 片段。它似乎没有连接到我的服务器,因为 handlefunc 从不注册故障排除 fmt println。
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)
}
此处的任何见解都将受到高度重视。非常感谢您审阅这个问题。
编辑:我忘了提到脚本上的警报已成功触发,因此点击脚本正在运行,而不是 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)
更新:在我对控制台进行了一些挖掘之后,这里的问题似乎是我使用的是 jquery 3.2.1 的精简版。切换到缩小的非 slim 版本后,我得到了服务器对查询的响应!我在这个应用程序上还有很多工作要做,但这是一个非常有值(value)的解决方案!非常感谢所有帮助我诊断这个问题的人!
最佳答案
It seems jQuery slim build, doesn't support ajax function. So include
jquery-3.2.1.min.js
try, it will work.
SO 帖子:了解 normal vs slim build .
你能试试这个 jquery ajax 调用吗?
$.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);
}
});
关于jquery - Ajax新手学习(golang jquery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44552209/