我是jquery的新手.我在django中使用jquery做一些ajax.
$(document).ready(function() {
$.getJSON('/jsonserver/', function(json){
alert("JSON Data: " + json);
});
Run Code Online (Sandbox Code Playgroud)
上面的jquery对我有用.当我在点击功能中添加一个按钮时,它会成功调用url,但不会显示警报.
$(document).ready(function() {
$('#save').click(function(){
$.getJSON('/jsonserver/', function(json){
alert("JSON Data: " + json);
});
});
});
<form method ="POST" method =/jqueryserver/ name ="form1" id ="form1">
<input type= "submit" id = "save" name ="save" value ="Save" />
</form>
Run Code Online (Sandbox Code Playgroud)
view.py
def jsonserver(request):
print "...jsonserver..."
json = simplejson.dumps('hello world!')
return HttpResponse(json, mimetype = 'application/json')
Run Code Online (Sandbox Code Playgroud)
blo*_*ead 10
由于$("#save")是提交按钮,单击该按钮将提交表单,并刷新页面.
你想做的是通过防止浏览器的默认事件被触发来"劫持".
这可以这样做:
$('#save').click(function(e) {
e.preventDefault();
$.getJSON('/jsonserver/', function(json){
alert("JSON Data: " + json);
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1983 次 |
| 最近记录: |