django - 无法将数据从 Angular 8 发布到 Django
问题描述
我正在尝试将数据从 Angular 发送到 Django,并且在浏览器中不断收到以下错误消息:
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost:8000/polls/token", ok: false, …}
error:
error: SyntaxError: Unexpected token N in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:32795:51) at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:6802:31) at Zone.runTask (http://localhost:4200/polyfills.js:6579:47) at ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:6876:34) at invokeTask (http://localhost:4200/polyfills.js:8014:14) at XMLHttpRequest.globalZoneAwareCallback (http://localhost:4200/polyfills.js:8051:21)
message: "Unexpected token N in JSON at position 0"
stack: "SyntaxError: Unexpected token N in JSON at position 0↵ at JSON.parse (<anonymous>)↵ at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:32795:51)↵ at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:6802:31)↵ at Zone.runTask (http://localhost:4200/polyfills.js:6579:47)↵ at ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:6876:34)↵ at invokeTask (http://localhost:4200/polyfills.js:8014:14)↵ at XMLHttpRequest.globalZoneAwareCallback (http://localhost:4200/polyfills.js:8051:21)"
__proto__: Error
text: "None"
__proto__: Object
headers: HttpHeaders
lazyInit: () => {…}
lazyUpdate: null
normalizedNames: Map(0) {}
__proto__: Object
message: "Http failure during parsing for http://localhost:8000/polls/token"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "http://localhost:8000/polls/token"
__proto__: HttpResponseBase
我也从 django 得到这个错误:
AttributeError at /polls/token
'NoneType' object has no attribute 'set_cookie'
这是我的 Angular 前端:
数据服务.ts:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
const httpOptions = {
withCredentials: true,
};
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(private http: HttpClient) { }
public sendPostRequest(data){
return this.http.post("//localhost:8000/polls/token", data, httpOptions);
}
}
这是 myapp.component.ts 的摘录:
...
constructor(
private dataService: DataService
) {}
...
this.auth2.attachClickHandler(
element,
{},
(googleUser) => {
let token = googleUser.getAuthResponse().id_token;
this.dataService.sendPostRequest(token).subscribe((data: any[])=>{
console.log(data);
});
},
(error) => {
alert(JSON.stringify(error, undefined, 2));
}
);
...
console.log(token)
返回一长串字母和数字
这是我的 Django 后端:
民意调查/views.py:
from django.conf import settings
from django.shortcuts import render
from django.http import HttpResponse, HttpRequest
from django.views.decorators.csrf import ensure_csrf_cookie
@ensure_csrf_cookie
def access_token(request: HttpRequest) -> HttpResponse:
if request.method == 'POST':
token = request.POST.get('body')
return HttpResponse(token)
民意调查/urls.py:
from django.conf import settings
from . import views
from django.urls import path
urlpatterns = [
path('token', views.access_token, name='access_token')
]
我是 django 和 angular 的新手,几天来一直在尝试解决这个问题。我似乎无法找出究竟是什么问题。
非常感谢您的帮助,谢谢!
解决方案
推荐阅读
- laravel - 为什么 docker 容器不在 Windows 中运行?
- c# - 摧毁后重生 AI
- google-apps-script - 带有 BigQuery 的 Google Workspace 插件,用于记录使用情况
- python - 使用python在excel中创建一个下拉列表
- html - 移动视图的下拉无序列表
- ios - 将 Swift 框架嵌入到另一个框架 Xcode
- java - JDK 8('1.8.*')的要求检查失败!检测到的版本:空
- python - Django-wkhtmltopdf - 动态模板
- microsoft-graph-api - 为用户卸载 ms 团队应用程序/机器人(机器人框架机器人)
- javascript - JS按特定字符串属性对对象数组进行排序(不升序或降序)