如何获取文本框字段的值并在data
内以ajaxOptions
的形式发送?我测试了我的视图,它从我的Django视图中成功打印出测试变量。我正在使用X-editable
作为Jquery。下面是文本框输入的样子:
http://jsfiddle.net/xBB5x/197/
views.py
def create_post(request):
print request.POST.get("test", "");
return HttpResponse(json,content_type="application/json")
HTML
<a id="other1" data-pk="First Name" data-name="test">First Name</a>
AJAX
$(document).ready(function() {
$('#other1').editable({
type: 'text',
pk: 1,
url: '/create_post/',
ajaxOptions: {
data: {
csrfmiddlewaretoken: '{{ csrf_token }}',
test: "hi",
},
},
placement: 'top',
title: 'New Expense',
success: function(response, newValue) {
if(response.status == 'error') return response.msg; //ms
},
});
});
答案 0 :(得分:0)
而不是ajaxOptions
,请使用params
。如果我没记错的话,test
及其值将通过x-editable包含在您的POST请求中。尝试这样的事情:
<强> HTML 强>
<a id="other1" data-pk="1" data-name="test">First Name</a>
<强> AJAX 强>
$(document).ready(function() {
$('#other1').editable({
type: 'text',
url: '/create_post/',
params : function(params) {
params.csrfmiddlewaretoken = '{{ csrf_token }}';
return params;
},
placement: 'top',
title: 'New Expense',
success: function(response, newValue) {
if(response.status == 'error') return response.msg; //ms
},
});
});