React 和 django-rest-framework 应用程序中的 401 错误
401 Error in React and django-rest-framework app
首先我得到了这个从输入中获取数据的 useState:
const Checkout = ({cart, totalPrice, code}) => {
const [values, setValues] = useState({
email: '',
city:'',
adress:'',
postalCode:'',
phone:'',
firstName:'',
lastName:'',
message:'',
price: totalPrice,
code: code,
shipmentFee:0,
shipmentMethod:'',
lockerId:'',
cart:cart,
});
然后我用generateOrder(values)
函数提交:
export const generateOrder = (order) => {
return fetch(`${url}/orders/generate-bank-transfer-order/`,{
method:"POST",
body:order
})
.then((response) => {
return response.json();
})
.catch((error) => console.log(error))
};
指向urls.py中的这个url:path('generate-bank-transfer-order/',generate_bank_transfer_order, name="generate-bank-transfer")
这是我使用的一个视图,现在我只想要它 return 提交的数据,这样我就可以测试它是否有效:
@csrf_exempt
def generate_bank_transfer_order(request):
if request.method == "POST":
body = request.body
return JsonResponse({"test":body})
我得到的只是 401 Unauthorized
,我不知道如何修复它。有什么想法吗?
如果您在以下设置中使用默认身份验证 class:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
]
}
第一次导入
from rest_framework.permissions import AllowAny
然后将您的函数修改为
@api_view(['POST'])
@permission_classes([AllowAny])
def generate_bank_transfer_order(request):
if request.method == "POST":
body = request.body
return JsonResponse({"test":body})
The AllowAny permission class will allow unrestricted access,
regardless of if the request was authenticated or unauthenticated.
This permission is not strictly required, since you can achieve the
same result by using an empty list or tuple for the permissions
setting, but you may find it useful to specify this class because it
makes the intention explicit.
首先我得到了这个从输入中获取数据的 useState:
const Checkout = ({cart, totalPrice, code}) => {
const [values, setValues] = useState({
email: '',
city:'',
adress:'',
postalCode:'',
phone:'',
firstName:'',
lastName:'',
message:'',
price: totalPrice,
code: code,
shipmentFee:0,
shipmentMethod:'',
lockerId:'',
cart:cart,
});
然后我用generateOrder(values)
函数提交:
export const generateOrder = (order) => {
return fetch(`${url}/orders/generate-bank-transfer-order/`,{
method:"POST",
body:order
})
.then((response) => {
return response.json();
})
.catch((error) => console.log(error))
};
指向urls.py中的这个url:path('generate-bank-transfer-order/',generate_bank_transfer_order, name="generate-bank-transfer")
这是我使用的一个视图,现在我只想要它 return 提交的数据,这样我就可以测试它是否有效:
@csrf_exempt
def generate_bank_transfer_order(request):
if request.method == "POST":
body = request.body
return JsonResponse({"test":body})
我得到的只是 401 Unauthorized
,我不知道如何修复它。有什么想法吗?
如果您在以下设置中使用默认身份验证 class:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
]
}
第一次导入
from rest_framework.permissions import AllowAny
然后将您的函数修改为
@api_view(['POST'])
@permission_classes([AllowAny])
def generate_bank_transfer_order(request):
if request.method == "POST":
body = request.body
return JsonResponse({"test":body})
The AllowAny permission class will allow unrestricted access, regardless of if the request was authenticated or unauthenticated.
This permission is not strictly required, since you can achieve the same result by using an empty list or tuple for the permissions setting, but you may find it useful to specify this class because it makes the intention explicit.