Теперь я хочу вызвать этот маршрут из ajax-запрос вне моего приложения:
Код: Выделить всё
http://127.0.0.1:3000/automation/get_data/sdfd
Код: Выделить всё
Route::get('/automation/get_data/{param1?}/{param2?}', [Automation::class, 'get_data']);
Код: Выделить всё
public function get_data ( Request $request ) {
$user = Auth::user();
echo '
';
var_dump( $user );
echo '';
echo 'get data method : access the data like this :
';
echo '';
print_r($request->route('param1'));
print_r($request->route('param2'));
echo '';
}
Код: Выделить всё
const accessToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vMTI3LjAuMC4xOjgwMDAvYXBpL2xvZ2luIiwiaWF0IjoxNzE1ODM4OTQ1LCJleHAiOjE3MTU4NDI1NDUsIm5iZiI6MTcxNTgzODk0NSwianRpIjoiOVAzM0FvYVBDZjBKbjhzUiIsInN1YiI6IjEiLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.Xx1bZXI808PKMzsKJFblY2WKSmDX2_ioC1oIvYjHaGo';
const url = 'http://127.0.0.1:3000/automation/get_data/sdfd';
const headers = new Headers({
'Authorization': `Bearer ${accessToken}`
});
fetch(url, {
method: 'GET',
headers: headers,
mode: 'no-cors' // Add this line
})
.then(response => {
if (!response.ok) { // Check for successful response status code
throw new Error(`API request failed with status ${response.status}`);
}
return response.json(); // Convert to JSON
})
.then(data => {
console.log("Received data from API:", data);
// Access data properties here (e.g., data.user_id, data.message, etc.)
})
.catch(error => {
console.error("Error:", error);
// Handle errors based on error type (network, response status, etc.)
});
Код: Выделить всё
index.html:33 Error: Error: API request failed with status 0
at index.html:24:15
Скажите, пожалуйста, как мне это сделать?
Подробнее здесь: https://stackoverflow.com/questions/784 ... er-details