BELOW是app.vue
vue应用的前端代码
<script setup>
import { ref } from 'vue';
const vmId = ref('/subscriptions/xxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM');
const isLoading = ref(false);
const message = ref('');
const turnOnVM = async () => {
isLoading.value = true;
message.value = '';
// Use https://<function-app-name>.azurewebsites.net/api/<function-name>
try {
const response = await fetch('http://localhost:7071/api/api/schedule', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ id: vmId.value }),
});
if (response.ok) {
const data = await response.text();
message.value = data;
} else {
throw new Error('Failed to turn on VM');
}
} catch (error) {
message.value = `Error: ${error.message}`;
} finally {
isLoading.value = false;
}
};
</script>
<template>
<header>
<img alt="Vue logo" class="logo" src="./assets/logo.svg" width="125" height="125" />
<div class="wrapper">
<h1>Azure VM Control</h1>
<button @click="turnOnVM" :disabled="isLoading">
{{ isLoading ? 'Turning On...' : 'Turn On VM' }}
</button>
<p v-if="message">{{ message }}</p>
</div>
</header>
<main>
<!-- Your main content here -->
</main>
</template>
<style scoped>
header {
line-height: 1.5;
}
.logo {
display: block;
margin: 0 auto 2rem;
}
@media (min-width: 1024px) {
header {
display: flex;
place-items: center;
padding-right: calc(var(--section-gap) / 2);
}
.logo {
margin: 0 2rem 0 0;
}
header .wrapper {
display: flex;
place-items: flex-start;
flex-wrap: wrap;
flex-direction: column;
}
button {
margin-top: 1rem;
padding: 0.5rem 1rem;
font-size: 1rem;
cursor: pointer;
}
p {
margin-top: 1rem;
color: green;
}
}
</style>
确保在locun.settings.json中的Azure函数应用中添加CORS。使用此
MMSDOC
的托管身份身份验证
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "python"
},
"Host": {
"LocalHttpPort": 7071,
"CORS": "*"
}
}