Javascript 我是不是遇到了CORS问题?
我已经按照教程设置了Laravel和Laravel Passport。 为了测试对api的调用,我在服务器上的不同目录中使用了此测试客户端:Javascript 我是不是遇到了CORS问题?,javascript,laravel,api,laravel-passport,Javascript,Laravel,Api,Laravel Passport,我已经按照教程设置了Laravel和Laravel Passport。 为了测试对api的调用,我在服务器上的不同目录中使用了此测试客户端: <?php require "vendor/autoload.php"; $client = new GuzzleHttp\Client; try { $response = $client->post('http://todos.test/oauth/token', [ 'form_params' => [
<?php
require "vendor/autoload.php";
$client = new GuzzleHttp\Client;
try {
$response = $client->post('http://todos.test/oauth/token', [
'form_params' => [
'client_id' => 2,
// The secret generated when you ran: php artisan passport:install
'client_secret' => 'fx5I3bspHpnuqfHFtvdQuppAzdXC7nJclMi2ESXj',
'grant_type' => 'password',
'username' => 'johndoe@scotch.io',
'password' => 'secret',
'scope' => '*',
]
]);
// You'd typically save this payload in the session
$auth = json_decode( (string) $response->getBody() );
$response = $client->get('http://todos.test/api/todos', [
'headers' => [
'Authorization' => 'Bearer '.$auth->access_token,
]
]);
$todos = json_decode( (string) $response->getBody() );
$todoList = "";
foreach ($todos as $todo) {
$todoList .= "<li>{$todo->task}".($todo->done ? '✅' : '')."</li>";
}
echo "<ul>{$todoList}</ul>";
} catch (GuzzleHttp\Exception\BadResponseException $e) {
echo "Unable to retrieve access token.";
}
不!
这不是CORS问题,您只是没有未经授权,请检查您的访问令牌是否有效且未过期。很抱歉,url不准确。我使用.test域。我如何检查访问令牌是否有效?如果出现身份验证错误,则自动表示您的访问令牌无效。这不是CORS的问题。要查看发送到后端的请求,请在页面上单击鼠标右键,然后单击Inspect元素,然后转到Network选项卡。单击XRH只查看ajax请求,现在您可以在这里看到您的请求响应、头、参数(如您正在发送的访问令牌)等等。