我試圖將使用javascript收到的數據發送到本地主機,但是當我將其構建爲android應用程序時,PHP文件無法運行。我試圖正常運行它在構建XAMP之前,它看起來像PHP連接即使數據沒有被髮送,但是在構建它作爲離子android應用程序之後,它甚至不能連接。這裏出了什麼問題?使用PHP從Ionic框架發佈數據
的index.html
<?php
include "main.php";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- START OF GEOLOCATION -->
<center><div class="round-button"><div class="round-button-circle"><a onclick= "getLocation()" class="round-button">HELP</a></div></div></center>
<p id="demo"></p>
<script src="js/jquery.js"></script>
<script>
var glob_latitude = '';
var glob_longitude = '';
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.watchPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";}
}
///send to ip
function showPosition(position) {
x.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
glob_longitude = position.coords.longitude;
glob_latitude = position.coords.latitude;
$.post("main.php", { latitude: glob_latitude, longitude: glob_longitude });
}
</script>
<!-- END OF GEOLOCATION -->
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app's js -->
<script src="js/app.js"></script>
</head>
<body ng-app="starter" background="css/style.css">
</body>
</html>
Main.php
<?php
echo "ok";
//$dbConnection = mysqli_connect("160.153.162.9", "Musab_Rashid" , "zaq123wsx" ,"Musab_Rme");
$dbConnection = mysqli_connect("localhost", "root" , "" ,"info");
echo "connected";
if($dbConnection)
{
echo "connected";
if(isset($_POST['latitude']) and isset($_POST['longitude'])){
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
if($latitude != '' and $longitude != '')
$query = mysqli_query("INSERT INTO info VALUES (NULL, '{$latitude}', '$longitude')");
}
}
else
die();
mysqli_close($dbConnection);
?>
Ionic是一個移動框架,所以我假設你將在移動設備上運行該應用程序。這意味着您需要將應用程序與服務器端分開(您需要在某個公共Web主機上運行該應用程序)。 – JimL
和KimL說的一樣,在提交你的ajax請求之後你需要做的另一件事情是,你必須在你的app.js –
中提取返回的值,但是當它沒有連接到本地主機時它不會給我一個錯誤,在移動結果上運行此代碼時會產生明顯的結果。這意味着應用程序無法運行PHP代碼,或者我錯了嗎? –