我在嘗試發送一個js變量給php,並用相同的變量進行查詢。通過AJAX發送變量到PHP
當我上標記點擊我得到Success
在控制檯上,但是當我嘗試使查詢我得到
Uncaught SyntaxError: Unexpected token < (index):105
(anonymous function) (index):105
request.onreadystatechange (index):185
線105 var data = JSON.parse(data.responseText);
線185 callback(request);
(makeRequest的部分)
JavaScript:
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
if (start.value.length <=0){
start.value=data[i].name;
start_cord.value ="("+(data[i].lat +", "+ data[i].lon)+")";
xpto = data[i].name;
$.ajax({
type: 'POST',
url: 'get_waypoint.php',
data: { var1: xpto },
cache: false,
success: function() {
console.log('Success')
}
});
get_waypoint.php:
require 'config.php';
$var = $_POST['var1'];
try {
$db = new PDO($dsn, $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $db->query("SELECT distinct * FROM horarios WHERE name ='$var'");
$locations = $sth->fetchAll();
echo json_encode($locations);
} catch (Exception $e) {
echo $e->getMessage();
}
makeRequest的:
function makeRequest(url, callback) {
var request;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest(); // IE7+, Firefox, Chrome, Opera, Safari
} else {
request = new ActiveXObject("Microsoft.XMLHTTP"); // IE6, IE5
}
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
callback(request);
}
}
request.open("GET", url, true);
request.send();
}
編輯
AJAX:
$.ajax({
url: 'get_waypoint.php',
type: 'POST',
data: {var1:start_wp, var2:end_wp},
dataType: 'JSON',
cache: false,
success: function(msg) {
console.log(msg)
console.log('Success')
for (var i = 0; i < msg.length; i++) {
waypts.push({location: new google.maps.LatLng(msg[i].lat, msg[i].lon),
stopover: true})
}
}
});
的誤差修改我看到的是那些我張貼在這裏。我不知道它是否有差別,但我忘了說用js腳本的文件是'index.html' – ingalion
是的,但我在談論PHP文件中的潛在錯誤,即在get_waypoint.php中。如果你可以訪問你的Ajax代碼請求的頁面(例如''get_waypoint.php'帶'var1') – ljacqu
,你的意思是'注意:未定義的索引:var1位於第3行的「location」\ get_waypoint.php?準確地說,在發送變量 – ingalion