【发布时间】:2013-01-25 20:23:02
【问题描述】:
我已经完成了设置,并且 dajaxproject.com 上的所有示例都运行良好,但我现在在使用我在更复杂的用例中学到的知识时遇到了问题。我想将几个参数以及来自表单的文本传递给 ajax 函数,并使用这些数据创建一个对象。
如果有人可以帮助我,将不胜感激。
我正在使用 jquery 和 jquery.ba-serializeobject.min.js。
Ajax.py:
@dajaxice_register
def save_comment(req, form, user_username, other_username):
dajax = Dajax()
comment_form = CreateCommentForm(deserialize_form(form))
if comment_form.is_valid():
text = comment_form.cleaned_data['text']
user = User.objects.get(username=user_username)
other_user = User.objects.get(username=other_username)
other_profile = Profile.objects.get(user=other_user)
comment = other_profile.comments.objects.create(owner=user, text=text)
comment.save()
return dajax.json()
JS:
<script type="text/javascript">
function add_comment(){
var user = '{{ person.user.username }}';
var other = '{{ other.user.username }}';
var data = $('#comment_form').serialize(true);
Dajaxice.profiles.save_comment(Dajax.process, {'form': data, 'user_username': user, 'other_username': other });
return false;
}
</script>
HTML:
<div><h4>Post Comment:</h4>
<div id="comment_form_errors"></div>
<form action="" id="comment_form" accept-charset="utf-8" method="post">
{% csrf_token %}
{{ commentform.as_p }}
<p><input class="btn profile-comment-submit" id="submit_profile_comment" onclick="add_comment()" type="submit" alt="register" /></p>
<form>
</div>
在 Chrome 的调试控制台中,我得到的唯一错误是 Dajaxice:出了点问题。
如果我遗漏了任何可能重要的内容,请告诉我。
非常感谢,
【问题讨论】:
-
这可能是由于您创建和传递 JSON 对象的方式所致。请在您的数据对象上尝试 JSON.stringify() 函数 (var data = JSON.stringify($('#comment_form').serialize(true)))。我没有使用过 JQuery,因此我可能会遗漏一些东西,但我有充分的理由怀疑这是由于您创建和传递 JSON 对象的方式造成的。
标签: python django jquery dajaxice dajax