剛剛學習php。我試圖通過AJAX調用連接PHP和Javascript。 我的JavaScript文件看起來像這樣:PHP文件返回整個文件
$.ajax({
type:"GET",
url:"test.php",
success:function(data) {
console.log(data); // to check response
}
});
和我的PHP文件看起來像:
<?php
include('example.php');
header("Content-type: text/javascript");
$key = '4e899892ede0f86b7cb65f974cede5ff';
$latitude = $_GET['latitude'];
$longitude = $_GET['longitude'];
$timestamp = $_GET['timestamp'];
$weather = new WeatherAPIConnector('4e899892ede0f86b7cb65f974cede5ff');
$condition = $weather->getCurrentForecast($latitude,$longitude,$timestamp);
echo $condition;
$conditi = array();
$conditi = $weather->getTodaysTemp($latitude,$longitude);
echo $conditi;
?>
控制檯顯示了整個PHP文件(執行console.log(數據)):
請讓我知道如何獲得單獨的$ conditi的價值
你必須在你的http服務器中啓用一個php模塊,或者將它配置爲通過「cgi」腳本執行或者「fastcgi」服務器將請求發送到php文件。如何做到這一點顯然取決於你使用的http服務器和你建立的操作系統。 – arkascha