【问题标题】:Accessing Html/php file having Js inside it using ajax使用 ajax 访问其中包含 Js 的 Html/php 文件
【发布时间】:2021-05-23 11:21:35
【问题描述】:

我有一个 index.php,我从中访问 noun_phrase.php 以返回一个变量。现在我必须将返回的变量从 noun_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_phrase.php 检索到的数据传递给 placeapi.php 的 javaScript 函数并获取位置,以便我可以在某处显示

【问题讨论】:

    标签: javascript php html jquery css


    【解决方案1】:

    您只需在构造函数 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

    【讨论】:

    • 嗯,我试过了,还是不行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多