[英]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.