Strange behavior when submitting post request in Angular 2

前端 未结 3 1830
春和景丽
春和景丽 2020-12-19 21:36

I realize my title is a bit vague, but here is my situation. I have just started an application in which I am implementing JWT for authentication. I have my server side se

相关标签:
3条回答
  • 2020-12-19 22:20

    Make sure that there is a trailing slash at the end of the route you are calling. So instead of calling

    'http://localhost:5000/auth'
    

    you target

    'http://localhost:5000/auth/'
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-19 22:27

    It looks like you're having problems with a cross origin request.
    The page host (localhost:4200) is different than the destination (localhost:5000).

    When that happens chrome issues a preflighted request:

    Unlike simple requests (discussed above), "preflighted" requests first send an HTTP request by the OPTIONS method to the resource on the other domain, in order to determine whether the actual request is safe to send. Cross-site requests are preflighted like this since they may have implications to user data.

    The response you get from there server doesn't include the needed CORS headers, and therefor chrome doesn't issue the actual POST request.

    0 讨论(0)
  • 2020-12-19 22:43

    As mentioned Chrome issues preflighted request. To bypass that you could install this Chrome extension to enable CORS and add *'Allow-Control-Allow-Origin: ' to your headers. That worked for me with django on localhost:8000 and angular2 on localhost:4200.

    0 讨论(0)
提交回复
热议问题