我正在使用Android Studio创建自己的(混合)应用程序,因此我正在使用HTML,CSS和Javascript。我还使用XAMPP创建LocalHost,并使用phpMyAdmin创建了数据库。我尝试使用javascript上的访存来连接数据库,但每次都会收到400错误请求。
那是我的dtbs.php
// --- Add Notitie
if (strcasecmp($_GET['m'], 'addNote') == 0) {
if (!$conn) {
$response['code'] = 0;
$response['status'] = $api_response_code[$response['code']]['HTTP Response'];
$response['data'] = mysqli_connect_error();
} else {
$response['code'] = 0;
$response['status'] = $api_response_code[$response['code']]['HTTP Response'];
$lQuery = "INSERT INTO notes (Note_ID, Title, Notitie) VALUES (NULL, '". $postvars['title'] . "'," . $postvars['notitie'].")";
$result = $conn -> query($lQuery);
$rows = array();
if (!$result) {
$response['data'] = "db error";
} else {
while ($row = $result -> fetch_assoc()) {
$rows[] = $row;
}
$response['code'] = 1;
$response['status'] = $api_response_code[$response['code']]['HTTP Response'];
$response['data'] = $rows;
}
}
}
在我的JavaScript中,我添加了以下内容:
//Create connection with dtbs
let apiAddress = "http://localhost/wm/dtbs.php?";
let opties = {
method: "POST", // *GET, POST, PUT, DELETE, etc.
mode: "cors", // no-cors, *cors, same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "omit", // include, *same-origin, omit
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
}
}
//Creating function for connecting to DTB and GET or PUT data.
//Put title and text into dtbs
function addNote() {
let url = apiAddress + "m=addNote";
// onze php api verwacht een paar parameters
// we voegen deze toe aan de body van de opties
// body data type must match "Content-Type" header
opties.body = JSON.stringify({
title: document.getElementById("makenewtitle").value,
notitie: document.getElementById("makenewnote").value,
format: "json"
});
// test de api
fetch(url, opties)
.then(function(response) {
return response.json();
})
.then(function(responseData){
// de verwerking van de data
var list = responseData.data;
})
.catch(function(error) {
// verwerk de fout
var alert = document.getElementById("alert")
alert.innerHTML = "fout : " + error;
});
}
有人可以帮我吗?
提前谢谢
That is what I get when I try to put title and text in my database