我使用 Phonegap 制作了一个移动应用程序。我使用 Ajax 从服务器获取数据。服务器有一个 Laravel 应用程序,以 Json 形式提供数据。这是代码:
jQuery(function($) {
$(document).ready(function() {
$(".loader").show();
$.ajax({
url: 'http://miIp/public/menu',
dataType: 'json',
success:function(data){
$("#conectionError").hide();
if(data == 1){
$('#liveStreaming').show();
$("#live_choose").show();
$(".btn_menu").hide();
}
if(data == 0){
$('#liveStreaming').hide();
$("#live_choose").hide();
$(".btn_menu").show();
$('#liveStreaming').hide();
}
$(".loader").fadeOut("slow");
},
error:function(){
$(".loader").hide();
var divHeight = $("#conectionError").height();
var imgHeight = $("#conectionErrorImg").height();
var imgMargin = (divHeight - imgHeight)/2;
$("#conectionErrorImg").css("margin-top", imgMargin);
$("#conectionError").show();
},
async: true
});
});
});
我在本地测试了该应用程序(我在 Xampp 中有 laravel 应用程序)并且工作正常。我什至使用phonegap build 编译了该应用程序,并在我的手机中测试了它,并且工作正常。
今天我将 Laravel 应用程序上传到 WIROO 上的云服务器,但移动应用程序没有获取数据。我把链接放在chrome上,可以看到Json中的数据,但是ajax一次又一次失败。
我想这可能是一个权限问题(我正在使用 Vesta Panel),但我对此一无所知。
¿有人可以帮助我吗?
<?php
require("conn.php");
// Fetch departments for dropdown
$departments = $conn->query("SELECT * FROM departments");
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$salutation = $_POST['salutation'];
$name = $_POST['name'];
$gender = $_POST['gender'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$department_id = $_POST['department'];
$doctor_id = $_POST['doctor'];
if(!preg_match('/^(\+91)?[6-9]\d{9}$/', $phone)) {
echo json_encode(['status' => 'error', 'message' => 'Invalid Phone Number ']);
exit;
}
if (!preg_match('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.(com|org)$/', $email)) {
echo json_encode(['status' => 'error', 'message' => 'Invalid email. It must end in .com or .org.']);
exit;
}
if (isset($_POST['action']) && $_POST['action'] == 'insert') {
$insert_query = "INSERT INTO patients (salutation, name, gender, phone, email, department_id, doctor_id) VALUES ('$salutation', '$name', '$gender','$phone', '$email', '$department_id', '$doctor_id')";
if ($conn->query($insert_query) === TRUE) {
echo json_encode(['status' => 'success', 'message' => 'Record inserted successfully']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Error inserting record: ' . $conn->error]);
}
exit;
} else if (isset($_POST['edit_id'])) {
$edit_id = $_POST['edit_id'];
$update_query = "UPDATE patients SET salutation = '$salutation', name = '$name', gender ='$gender', phone = '$phone', email = '$email', department_id = '$department_id', doctor_id = '$doctor_id' WHERE id = '$edit_id'";
if ($conn->query($update_query) === TRUE) {
echo json_encode(['status' => 'success', 'message' => 'Record updated successfully']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Error updating record: ' . $conn->error]);
}
exit;
} else if (isset($_POST['del_id'])) {
$id = $_POST['del_id'];
$query = "DELETE FROM patients WHERE id = $id";
if ($conn->query($query) === TRUE) {
echo json_encode(['status' => 'success', 'message' => 'Record deleted successfully']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Error inserting record: ' . $conn->error]);
}
exit;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Patient Form</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="container mt-5">
<h2 class="text-center">Patient Registration</h2>
<form id="patientForm">
<div class="form-row">
<div class="form-group col-md-2">
<label for="salutation">Salutation</label>
<select id="salutation" name="salutation" class="form-control " required>
<option>Mr</option>
<option>Mrs</option>
<option>Ms</option>
<option>Dr</option>
</select>
</div>
<div class="form-group col-md-5">
<label for="name">Full Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter Full Name" required>
</div>
<div>
<lable >Gender</lable><br>
<input type="radio" id="gender" name="gender" value="Male" required>
<label for="gender">Male</label>
<input type="radio" id="gender" name="gender" value="Female" required>
<label for="gender">Female</label>
<input type="radio" id="gender" name="gender" value="Others" required>
<label for="gender">Others</label>
</div>
<div class="form-group col-md-5">
<label for="phone">Phone</label>
<input type="text" class="form-control" id="phone" name="phone" placeholder="Phone Number" required>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Email" required>
</div>
<div class="form-group col-md-3">
<label for="department">Department</label>
<select id="department" name="department" class="form-control" required>
<option value="">Select Department</option>
<?php while ($row = $departments->fetch_assoc()) { ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['department_name']; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group col-md-3">
<label for="doctor">Doctor</label>
<select id="doctor" name="doctor" class="form-control" required>
<option value="">Select Doctor</option>
</select>
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<h3 class="text-center mt-5">Patient Records</h3>
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Gender</th>
<th>Phone</th>
<th>Email</th>
<th>Department</th>
<th>Doctor</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="patientTableBody">
<!-- Table rows to be populated via AJAX -->
</tbody>
</table>
</div>
<script>
$(document).ready(function() {
// Fetch doctors based on department
$('#department').change(function() {
var departmentId = $(this).val();
$.ajax({
url: 'fetch_doctors.php',
type: 'POST',
data: { department_id: departmentId },
success: function(data) {
$('#doctor').html(data);
}
});
});
// Submit form via AJAX
$('#patientForm').submit(function(e) {
e.preventDefault();
var action = $('#edit_id').length ? 'update' : 'insert';
$.ajax({
url: 'index.php',
type: 'POST',
data: $(this).serialize() + '&action=' + action,
success: function(response) {
var res = JSON.parse(response);
alert(res.message);
if (res.status == 'success') {
$('#patientForm')[0].reset();
let doctorOptions = '<option value="">Select Doctor</option>';
$('#doctor').html(doctorOptions);
$('#patientForm').find('#edit_id').remove();
loadTable();
}
}
});
});
$(document).on('click', '.btn-edit', function() {
var id = $(this).data('id');
$.ajax({
url: 'fetch_patients.php',
type: 'POST',
data: { id: id },
success: function(response) {
var data = JSON.parse(response);
$('#salutation').val(data.salutation);
$('#name').val(data.name);
$("#gender[value=" + data.gender + "]").prop('checked', true);
$('#phone').val(data.phone);
$('#email').val(data.email);
$('#department').val(data.department_id).change();
$('#doctor').val(data.doctor_id);
if ($('#edit_id').length === 0) {
$('<input>').attr({
type: 'hidden',
id: 'edit_id',
name: 'edit_id',
value: id
}).appendTo('#patientForm');
}
}
});
});
$(document).on('click', '.btn-delete', function() {
var id = $(this).data('id');
$.ajax({
url: 'index.php',
type: 'POST',
data: $(this).serialize() + '&del_id=' + id,
success: function(response) {
var data = JSON.parse(response);
alert(data.message);
loadTable();
}
});
});
// Load patient records
function loadTable() {
$.ajax({
url: 'fetch_patients.php',
type: 'GET',
success: function(data) {
$('#patientTableBody').html(data);
}
});
}
loadTable(); // Initial table load
});
</script>
</body>
</html>
------
<?php
require("conn.php");
if(isset($_POST['id'])) {
$id = $_POST['id'];
$query = "SELECT * FROM patients WHERE id = $id ORDER BY id ";
$result = $conn->query($query);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo json_encode([
'id' => $row['id'],
'salutation' => $row['salutation'],
'name' => $row['name'],
'gender' => $row['gender'],
'phone' => $row['phone'],
'email' => $row['email'],
'department_id' => $row['department_id'],
'doctor_id' => $row['doctor_id']
]);
} else {
echo json_encode(['status' => 'error', 'message' => 'Record not found']);
}
} else {
$patients = $conn->query("SELECT p.id, CONCAT(p.salutation, ' ', p.name) AS name,p.gender, p.phone, p.email, d.department_name, doc.doctor_name
FROM patients p
JOIN departments d ON p.department_id = d.id
JOIN doctors doc ON p.doctor_id = doc.id ORDER BY p.id");
while ($row = $patients->fetch_assoc()) {
echo "<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['gender']}</td>
<td>{$row['phone']}</td>
<td>{$row['email']}</td>
<td>{$row['department_name']}</td>
<td>{$row['doctor_name']}</td>
<td>
<button class='btn btn-info btn-edit' data-id='{$row['id']}'>Edit</button>
<button class='btn btn-danger btn-delete' data-id='{$row['id']}'>Delete</button>
</td>
</tr>";
}
}
?>