【问题标题】:django-cors-headers is not working when the ID is added to the URL将 ID 添加到 URL 时,django-cors-headers 不起作用
【发布时间】:2019-08-12 19:12:00
【问题描述】:

除非我将成员 ID 添加到 URL,否则一切正常。有谁知道为什么?提前谢谢你。

工作正常:

componentDidMount(){
    axios.get('http://localhost:8000/smsf/smsf_member/')
        .then(response =>{
            this.setState({members: response.data.results});
           console.log(response);
        });
}

将会员 ID 添加到 URL 后不起作用

componentDidUpdate(){
    if(this.props.id){
        console.log(this.props.id);
        axios.get('http://localhost:8000/smsf/smsf_member/' + this.props.id)
            .then(response =>{
                console.log(response);
            });
    }
}

url.py

router = routers.DefaultRouter()
router.register(r'staff_member', StaffMemberViewSet)
router.register(r'smsf_member', SMSFMemberViewSet)
router.register(r'documents', DocumentsViewSet)

urlpatterns = [
path('', include(router.urls)),
path('api-token-auth/', obtain_auth_token, name='api_token_auth'),
]

view.py

class SMSFMemberViewSet(viewsets.ModelViewSet):
    queryset = SMSFMember.objects.all()
    serializer_class = SMSFMemberSerializer

settings.py

CORS_ORIGIN_ALLOW_ALL = True

错误信息:

【问题讨论】:

    标签: django reactjs axios django-cors-headers


    【解决方案1】:

    我认为这可能是因为第二次尝试 URL 没有以 / 结尾

    我过去遇到过类似的问题。

    查看:https://docs.djangoproject.com/en/2.1/ref/settings/#append-slash

    也许这样并重新排序您的中间件可以解决问题。

    祝你好运!

    【讨论】:

    • 是的,你是对的。太感谢了。我花了很多时间在这上面
    • 欢迎您@RonildoBragaJunior!请解决问题,使其成为选定的答案,以便其他人可以轻松访问该解决方案。
    猜你喜欢
    • 2015-03-18
    • 2017-08-11
    • 2018-08-21
    • 2016-06-08
    • 2016-11-16
    • 2016-09-29
    • 2018-06-23
    • 2018-04-24
    • 2018-01-16
    相关资源
    最近更新 更多