我正在使用 html、css 和 javascript 在cordova 中構建我的第一個應用程式。我現在想要做的是,在單擊按鈕時,呼叫本地服務器,并讓它記錄一些東西,這樣我就知道它可以作業。但是,即使這樣也行不通。所以我想要做的是如下:
索引.html
<body>
<div class="app">
<div class="login-box">
<h2>Login</h2>
<form>
<div class="user-box">
<input type="text" name="" required="">
<label>Username</label>
</div>
<div class="user-box">
<input type="password" name="" required="">
<label>Password</label>
</div>
<span></span>
<span></span>
<span></span>
<span></span>
<button onclick="submitButton()">Submit Function</button>
</form>
</div>
</div>
<script src="cordova.js"></script>
<script src="js/index.js"></script>
然后在我的index.js檔案中,我有這個:
function submitButton() {
const url ='http://localhost:6969/test';
const headers = {
"Content-Type": "application/json",
}
fetch(url, { method: 'POST', headers: headers})
.then((res) => {
return res.json()
})
}
在我的服務器中:
app.post('/test', (req, res) => {
console.log("this is actually pretty cool, if it worked!")
})
我究竟做錯了什么?我是否在 JS 方面或科爾多瓦錯過了一些東西?
uj5u.com熱心網友回復:
您可以將submitButton 中的fetch API更新為此嗎?
fetch(url, {
method: 'POST',
headers: headers,
// body: JSON.stringify(data), // you can just skip this if all you want is to test the API
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
})
.catch((error) => {
console.error('Error:', error);
});
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/400212.html
標籤:javascript html 节点.js 科尔多瓦 科尔多瓦插件
