无法 return json 从 django 视图到 axios
unable to return json from django view to axios
我想制作一个视觉搜索界面,带有关键字和类别来搜索某物。显然,它需要前后端数据交互。所以权衡了一些工具后,我选择了Vue和axios来实现前端的数据交互],以及 django view 在 front-end.
在我的 index.html 页面中,我这样定义了我的 axios。
var param = new URLSearchParams();
param.append('searchKey',this.searchKey);
param.append('category',this.selected);
axios.post("{% url 'main:getCommodityInfo'%}",
param,
{headers:{'X-CSRFToken': this.getCookie('csrftoken')}},)
.then(response=>{
console.log(response);
})
.catch(error=>{
console.log(error);
alert("connection has error")
})
searchKey 和 category 可以被 Vue 对象获取。然后我 post 数据到我的后端 django 视图。
def getCommodityInfo(request):
if request.method=="POST":
# To get the POST paramaters
searchKey = request.POST.get('searchKey')
category = request.POST.get('category')
# unique ID for each record for DB
uniqueId = str(uuid4())
print("Enter the view! ",searchKey,category)
# set setting
settings = {
'unique_id': uniqueId,
'USER_AGENT': 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
}
# task Id to indentify each spider task
task = scrapyd.schedule('JDSpider', 'getCommodityInfo',
settings=settings, searchKey=searchKey, category=category)
print("It seems everything is running well? ")
print(task,uniqueId)
print("-"*100)
return JsonResponse({'taskId': task, 'uniqueId': uniqueId, 'status': 'started'},safe=False)
好吧,视图实际上可以获取 POST 参数。 但是当我尝试 return 从视图到 axios 函数的状态 json 时,发生错误。
[20/Mar/2019 21:03:08] "GET /main/ HTTP/1.1" 200 7216
[20/Mar/2019 21:03:08] "GET /static/main/axios.min.js HTTP/1.1" 304 0
[20/Mar/2019 21:03:08] "GET /static/main/axios.min.map HTTP/1.1" 304 0
Enter the view! switch Electronics
[20/Mar/2019 21:03:30] "GET /main/?searchKey=switch&category=Electronics HTTP/1.1" 200 7216
It seems everything is running well?
8fdcee984b1011e9b7e5ace010528bab e07cd1ee-efe8-4c23-8457-51732aa57435
----------------------------------------------------------------------------------------------------
[20/Mar/2019 21:03:33] "POST /main/getCommodityInfo/ HTTP/1.1" 200 119
Traceback (most recent call last):
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 138, in run
self.finish_response()
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 332, in send_headers
self.send_preamble()
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 255, in send_preamble
('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 453, in _write
result = self.stdout.write(data)
File "D:\Anacaonda\lib\socketserver.py", line 775, in write
self._sock.sendall(b)
ConnectionAbortedError: [WinError 10053] The software in your host has aborted an established connection.
[20/Mar/2019 21:03:33] "POST /main/getCommodityInfo/ HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 11890)
Traceback (most recent call last):
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 138, in run
self.finish_response()
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 332, in send_headers
self.send_preamble()
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 255, in send_preamble
('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 453, in _write
result = self.stdout.write(data)
File "D:\Anacaonda\lib\socketserver.py", line 775, in write
self._sock.sendall(b)
ConnectionAbortedError: [WinError 10053] The software in your host has aborted an established connection.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 141, in run
self.handle_error()
File "D:\Anacaonda\lib\site-packages\django\core\servers\basehttp.py", line 86, in handle_error
super().handle_error()
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 368, in handle_error
self.finish_response()
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 344, in client_is_modern
return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Anacaonda\lib\socketserver.py", line 639, in process_request_thread
self.finish_request(request, client_address)
File "D:\Anacaonda\lib\socketserver.py", line 361, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "D:\Anacaonda\lib\socketserver.py", line 696, in __init__
self.handle()
File "D:\Anacaonda\lib\site-packages\django\core\servers\basehttp.py", line 154, in handle
handler.run(self.server.get_app())
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 144, in run
self.close()
File "D:\Anacaonda\lib\wsgiref\simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
我试过调试流程,但是代码逻辑太复杂,看不懂。 我只能发现执行时出现错误
return JsonResponse({'taskId': task, 'uniqueId': uniqueId, 'status': 'started'},safe=False)
我在网上找了很久。但是没有用。请帮助或尝试提供一些想法如何提前实现this.Thanks。
我环顾四周,显然我有以下选择。
<form action="{%url 'main:getCommodityInfo'%}" method="POST">
使用表单可以在我测试后从django View中获取json。但是我不想在我的项目中使用它而不是 axios。
您获得的堆栈跟踪是 Windows 上 wsgiref BaseHandler
的一个已知错误。
您可以在这里跟踪它:
https://github.com/python/cpython/pull/9713
这里:
我想制作一个视觉搜索界面,带有关键字和类别来搜索某物。显然,它需要前后端数据交互。所以权衡了一些工具后,我选择了Vue和axios来实现前端的数据交互],以及 django view 在 front-end.
在我的 index.html 页面中,我这样定义了我的 axios。
var param = new URLSearchParams();
param.append('searchKey',this.searchKey);
param.append('category',this.selected);
axios.post("{% url 'main:getCommodityInfo'%}",
param,
{headers:{'X-CSRFToken': this.getCookie('csrftoken')}},)
.then(response=>{
console.log(response);
})
.catch(error=>{
console.log(error);
alert("connection has error")
})
searchKey 和 category 可以被 Vue 对象获取。然后我 post 数据到我的后端 django 视图。
def getCommodityInfo(request):
if request.method=="POST":
# To get the POST paramaters
searchKey = request.POST.get('searchKey')
category = request.POST.get('category')
# unique ID for each record for DB
uniqueId = str(uuid4())
print("Enter the view! ",searchKey,category)
# set setting
settings = {
'unique_id': uniqueId,
'USER_AGENT': 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
}
# task Id to indentify each spider task
task = scrapyd.schedule('JDSpider', 'getCommodityInfo',
settings=settings, searchKey=searchKey, category=category)
print("It seems everything is running well? ")
print(task,uniqueId)
print("-"*100)
return JsonResponse({'taskId': task, 'uniqueId': uniqueId, 'status': 'started'},safe=False)
好吧,视图实际上可以获取 POST 参数。 但是当我尝试 return 从视图到 axios 函数的状态 json 时,发生错误。
[20/Mar/2019 21:03:08] "GET /main/ HTTP/1.1" 200 7216
[20/Mar/2019 21:03:08] "GET /static/main/axios.min.js HTTP/1.1" 304 0
[20/Mar/2019 21:03:08] "GET /static/main/axios.min.map HTTP/1.1" 304 0
Enter the view! switch Electronics
[20/Mar/2019 21:03:30] "GET /main/?searchKey=switch&category=Electronics HTTP/1.1" 200 7216
It seems everything is running well?
8fdcee984b1011e9b7e5ace010528bab e07cd1ee-efe8-4c23-8457-51732aa57435
----------------------------------------------------------------------------------------------------
[20/Mar/2019 21:03:33] "POST /main/getCommodityInfo/ HTTP/1.1" 200 119
Traceback (most recent call last):
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 138, in run
self.finish_response()
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 332, in send_headers
self.send_preamble()
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 255, in send_preamble
('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 453, in _write
result = self.stdout.write(data)
File "D:\Anacaonda\lib\socketserver.py", line 775, in write
self._sock.sendall(b)
ConnectionAbortedError: [WinError 10053] The software in your host has aborted an established connection.
[20/Mar/2019 21:03:33] "POST /main/getCommodityInfo/ HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 11890)
Traceback (most recent call last):
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 138, in run
self.finish_response()
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 332, in send_headers
self.send_preamble()
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 255, in send_preamble
('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 453, in _write
result = self.stdout.write(data)
File "D:\Anacaonda\lib\socketserver.py", line 775, in write
self._sock.sendall(b)
ConnectionAbortedError: [WinError 10053] The software in your host has aborted an established connection.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 141, in run
self.handle_error()
File "D:\Anacaonda\lib\site-packages\django\core\servers\basehttp.py", line 86, in handle_error
super().handle_error()
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 368, in handle_error
self.finish_response()
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 344, in client_is_modern
return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Anacaonda\lib\socketserver.py", line 639, in process_request_thread
self.finish_request(request, client_address)
File "D:\Anacaonda\lib\socketserver.py", line 361, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "D:\Anacaonda\lib\socketserver.py", line 696, in __init__
self.handle()
File "D:\Anacaonda\lib\site-packages\django\core\servers\basehttp.py", line 154, in handle
handler.run(self.server.get_app())
File "D:\Anacaonda\lib\wsgiref\handlers.py", line 144, in run
self.close()
File "D:\Anacaonda\lib\wsgiref\simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
我试过调试流程,但是代码逻辑太复杂,看不懂。 我只能发现执行时出现错误
return JsonResponse({'taskId': task, 'uniqueId': uniqueId, 'status': 'started'},safe=False)
我在网上找了很久。但是没有用。请帮助或尝试提供一些想法如何提前实现this.Thanks。
我环顾四周,显然我有以下选择。
<form action="{%url 'main:getCommodityInfo'%}" method="POST">
使用表单可以在我测试后从django View中获取json。但是我不想在我的项目中使用它而不是 axios。
您获得的堆栈跟踪是 Windows 上 wsgiref BaseHandler
的一个已知错误。
您可以在这里跟踪它:
https://github.com/python/cpython/pull/9713
这里: