![](/img/trans.png)
[英]Getting 500 error on simple form POST via AJAX — not sure what's wrong?
[英]500 server error, I have no idea what's wrong, ajax, django
我有这些代码,已经花了几个小时了,但是我不知道出了什么问题。
我一直收到500个服务器的响应,而当我触发ajax时,它甚至都不会在视图定义中开始调试。
我真的很茫然,任何帮助都太棒了!
$('.cheque_info_edit_button').live('click', function(){
var new_cheque = {
// cheque number is taken from the cell, not input box for this one.
cheque_no: $(this).closest('td').closest('tr').find('.inv_prof_cheque_no').text(),
their_bank: $(this).closest('td').closest('tr').find('.new_their_bank_input_ajax').val(),
our_bank: $(this).closest('td').closest('tr').find('.new_our_bank_input_ajax').val(),
cash_in_date: $(this).closest('td').closest('tr').find('.new_cash_in_date_input_ajax').val(),
cheque_amount: $(this).closest('td').closest('tr').find('.new_cheque_amount_input_ajax').val(),
info_type: 'edit'
};
var cheque_json = JSON.stringify(new_cheque);
$.ajax({
type: 'POST',
url: '/best_choose/invoice/profile/inv_add_or_edit_cheque/',
data: cheque_json,
success: function(){
// do stuff
}
更新:我不认为语法上我的观点有什么问题,因此我将其删除并添加了回溯,csrf令牌有问题吗? 我所有其他的ajax函数都起作用
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/best_choose/invoice/profile/inv_add_or_edit_cheque/
Django Version: 1.3
Python Version: 2.7.2
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'SY_SYSTEM.sy_system',
'django.contrib.humanize']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
178. response = middleware_method(request, response)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/middleware/csrf.py" in process_response
287. response.content, n = _POST_FORM_RE.subn(add_csrf_field, response.content)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/http/__init__.py" in _get_content
596. return smart_str(''.join(self._container), self._charset)
Exception Type: TypeError at /best_choose/invoice/profile/inv_add_or_edit_cheque/
Exception Value: sequence item 0: expected string, NoneType found
我看到了您的jquery代码,应该没有任何错误...
只是一个疯狂的猜测,因为之前发生过类似的事情,并且花了我一段时间才意识到(忘记了回溯的样子)
您可能要检查您的url文件,并确保没有使用相同的url或现有url模式的通配符。
从我所看到的,您的回溯:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/http/__init__.py" in _get_content
596. return smart_str(''.join(self._container), self._charset)
Exception Type: TypeError at /best_choose/invoice/profile/inv_add_or_edit_cheque/
Exception Value: sequence item 0: expected string, NoneType found
表示''.join(self._container)
部分遇到的元素不是字符串(NoneType),即,该表单的某些部分以NoneType结尾并且在应该添加CSRF字段时无法正确序列化。 查看Django代码self._container
实际上是HttpResponse传入的内容。这意味着Django尝试在响应的表单中添加csrfmiddletoken字段,并尝试以某种形式匹配该表单并将其插入的正则表达式最终捕获了NoneType而不是只是字符串。
正则表达式为:
_POST_FORM_RE = \
re.compile(r'(<form\W[^>]*\bmethod\s*=\s*(\'|"|)POST(\'|"|)\b[^>]*>)', re.IGNORECASE)
我会检查您所生成的响应表,并确保其格式正确。 这也将有助于单步执行csrf代码,并准确查看其捕获NoneType的位置。
如果不查看视图处理程序和表单代码,我真的不能多说。
您的目标视图应包含以下模式,以使AJAX发布请求正常进行:
def target_view(request)如果request.method =='POST':如果request.is_ajax():
jQuery的.load()和.get()方法可以正常工作,但要使用$ .ajax()或$ .post()方法,则必须在目标函数中遵循上述模式。
这是我的js源函数:
$.ajax({
type: 'POST',
url: '/target view url/',
success: function(data){
alert('it works');
}
});
如果失败请回复我。
当请求不是ajax时,尝试回滚。 使用commit_manually装饰器时,您需要回滚或提交。
您是否已将这个带有csrf保护功能的代码添加到带有ajax POST请求的所有页面中?
$(document).ready(function(){
$.ajaxSetup({
beforeSend: function(xhr, settings){
function getCookie(n) {
var cookieValue = null;
if(document.cookie&&document.cookie != ''){
var cookies = document.cookie.split(';');
for(var i = 0; i < cookies.length; i++){
var cookie = jQuery.trim(cookies[i]);
if(cookie.substring(0, n.length + 1) == (n + '=')){
cookieValue = decodeURIComponent(cookie.substring(n.length + 1));
break;
}
}
}
return cookieValue;
}
if(!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))){
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
}
});
});
在docs中有解释 。
试试这个,请提供一些反馈。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.