无法找到任何相关文档,因此在我深入研究代码之前,是否有人知道在使用“fetch”发出 REST 请求时如何使用基本身份验证(https://github.com/github/fetch)。
\n\n刚刚尝试了以下行,但请求中未设置标头:
\n\n fetch(\'http://localhost:8080/timeEntry\', {\n mode: \'no-cors\',\n headers: { \'Authorization\': \'Basic YW5kcmVhczpzZWxlbndhbGw=\' }\n })\n .then(checkStatus)\n .then(parseJSON)\n .then(function(activities) {\n console.log(\'request succeeded with JSON response\', data);\n dispatch(activitiesFetched(activities, null));\n }).catch(function(error) {\n console.log(\'request failed\', error);\n dispatch(activitiesFetched(null, error));\n });\n
Run Code Online (Sandbox Code Playgroud)\n\n用户名和密码是我自己的名字和姓氏,使用curl
它就可以了。
如果我设置了{\xc2\xa0\'Accept\' : \'application/test\' }
接受,只是不Authorization
……奇怪了。
只是为了我能够继续我添加credentials: \'include\'
这使得浏览器提示输入用于与 REST 后端通信的用户名和密码。仅用于测试,稍后将使用 OAuth。
fetch(\'http://localhost:8080/timeEntry\', {\n mode: \'no-cors\',\n credentials: \'include\'\n })\n .then(checkStatus)\n .then(parseJSON)\n .then(function(activities) {\n console.log(\'request succeeded with JSON response\', data);\n dispatch(activitiesFetched(activities, null));\n …
Run Code Online (Sandbox Code Playgroud)