我試圖使用Ajax發送數據,但是變量input_text,rotate總是採用默認情況下的值(input_text ='',rotate = 0)。 如果使用request.POST [ '文本'],然後我得到一個錯誤500 request.POST是空 異常類型:MultiValueDictKeyError在/加密 異常值: 「 '文本'」 views.py:數據沒有使用jQuery發送到Django視圖
def encrypt(request):
input_text = request.POST.get('text','')
rotate = request.POST.get('rotate', 0)
output_text = models.encode(input_text, rotate)
frequency = models.get_frequency(input_text)
key = models.get_key(input_text)
data = {'input_text': input_text, 'rotate': rotate, 'output_text': output_text, 'frequency': frequency, 'key': key}
return HttpResponse(json.dumps(data))
home.html做爲:
$("#encrypt").click(function() {
var postData = {
text: $("#input-box").val(),
rotate: $("#rotate").val()
};
$.ajax({
url: "encrypt",
type: "POST",
data: postData,
dataType: "json",
contentType: "application/json",
success: function(data) {
console.log(data);
$("#input-box").val(data.input_text);
},
error : function(xhr,errmsg,err) {
console.log(xhr.status + ": " + xhr.responseText);
}
});
return false;
});
控制檯如果request.POST [ '文本']:
500: MultiValueDictKeyError at /encrypt
"'text'"
Request Method: POST
Request URL: http://127.0.0.1:8000/encrypt
Django Version: 1.9.5
Python Executable: C:\Program Files (x86)\Python35-32\python.exe
Python Version: 3.5.1
Python Path: ['C:\\Users\\Влад\\PycharmProjects\\CezarProject', 'C:\\Users\\Влад\\PycharmProjects\\CezarProject', 'C:\\Program Files (x86)\\Python35-32\\python35.zip', 'C:\\Program Files (x86)\\Python35-32\\DLLs', 'C:\\Program Files (x86)\\Python35-32\\lib', 'C:\\Program Files (x86)\\Python35-32', 'C:\\Program Files (x86)\\Python35-32\\lib\\site-packages']
Server time: Tue, 12 Apr 2016 15:17:12 +0300
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'caesar']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\utils\datastructures.py" in __getitem__
83. list_ = super(MultiValueDict, self).__getitem__(key)
During handling of the above exception ('text'), another exception occurred:
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\handlers\base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\handlers\base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Влад\PycharmProjects\CezarProject\caesar\views.py" in encrypt
12. input_text = request.POST['text']
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\utils\datastructures.py" in __getitem__
85. raise MultiValueDictKeyError(repr(key))
Exception Type: MultiValueDictKeyError at /encrypt
Exception Value: "'text'"
Request information:
GET: No GET data
POST: No POST data
FILES: No FILES data
控制檯如果熱曲est.POST.get( '文本'):
500: TypeError at /encrypt
'NoneType' object is not iterable
Request Method: POST
Request URL: http://127.0.0.1:8000/encrypt
Django Version: 1.9.5
Python Executable: C:\Program Files (x86)\Python35-32\python.exe
Python Version: 3.5.1
Python Path: ['C:\\Users\\Влад\\PycharmProjects\\CezarProject', 'C:\\Users\\Влад\\PycharmProjects\\CezarProject', 'C:\\Program Files (x86)\\Python35-32\\python35.zip', 'C:\\Program Files (x86)\\Python35-32\\DLLs', 'C:\\Program Files (x86)\\Python35-32\\lib', 'C:\\Program Files (x86)\\Python35-32', 'C:\\Program Files (x86)\\Python35-32\\lib\\site-packages']
Server time: Tue, 12 Apr 2016 15:18:28 +0300
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'caesar']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\handlers\base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\handlers\base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Влад\PycharmProjects\CezarProject\caesar\views.py" in encrypt
14. output_text = models.encode(input_text, rotate)
File "C:\Users\Влад\PycharmProjects\CezarProject\caesar\models.py" in encode
14. for char in input_text:
Exception Type: TypeError at /encrypt
Exception Value: 'NoneType' object is not iterable
Request information:
GET: No GET data
POST: No POST data
FILES: No FILES data
您可以發佈你的HTML也是。 –
也發佈您的console.log數據 – Mani
@MuhammadShoaib已更新 –