使用ajax访问包含Js的Html.php文件



我有一个index.php,我从那里访问noun_phreph.php以返回一个变量。现在,我必须将返回的变量从non_phrase.php传递到另一个php文件placeapi.php以执行特定任务。

index.php

if(dialog.indexOf("restaurant")!=-1 ){
$.ajaxSetup({async: false});
showText="";
sayText="";
$.ajax({
url: "noun_phrase.php",
data: {text: dialog},
success: function(data) {
nlp = data;
findPlace(nlp);
}
});
}
function findPlace(x){
name = x;
$.ajax({
url: "placeapi.php",
data: {text: name},
success: function (data){
}
});
}

placeapi.php



<!DOCTYPE html>
<html>
<head>
<title>Place searches</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCmrjT6f4nt15qV-L9MRWL43TgucZzJHCw
&libraries=places"></script>
<script>
var map;
var infowindow;
function initialize() {
var city = new google.maps.LatLng(-42.882391,147.328591);
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: city,
zoom: 15
});
var request = {
location: city,
radius: 500,
types: ['hotel']
};
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);
}
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas">
</div>
</body>
</html>

我想将从noun_phase.php检索到的数据传递到placeapi.php的javaScript函数,并获取位置,以便我可以在某个地方显示

您只需在构造函数google.maps.LatLng中回显该值

function initialize() {
var city = new google.maps.LatLng(<?php echo $_POST['latitude'] ?>,<?php echo $_POST['latitude'] ?>); 
// longitude and latitude are part of `npl`
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: city,
zoom: 15
});
var request = {
location: city,
radius: 500,
types: ['hotel']
};

确保在ajax 中添加method:POST

如果您想通过javascript访问,请向noun_phrase.php发送get请求。并回显或返回noun_phrase.php的响应并从placeapi.php获得此响应

最新更新