Преглед на файлове

Ajout d'un proxy local pour l'éxécution des requêtes Ajax (corrige
l'erreur CROS)

omassot преди 6 години
родител
ревизия
8d9c335307
променени са 2 файла, в които са добавени 72 реда и са изтрити 8 реда
  1. 63 0
      ajax/geocode.php
  2. 9 8
      js/app.js

+ 63 - 0
ajax/geocode.php

@@ -0,0 +1,63 @@
+<?php
+  ini_set('display_errors', 1);
+  ini_set('display_startup_errors', 1);
+  error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
+  
+  if(isset($_GET['text'])) {
+
+      $url = 'http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/suggest';
+      $data = array('f' => 'pjson', 
+                    'searchExtent' => '{"xmin":-0.5,"ymin":49.732307856286,"xmax":-1.97117692088061,"ymax":48.4,"spatialReference":{"wkid":84}}',
+          'text' => $_GET['text']
+      );
+      
+      //url-ify the data for the POST
+      $url_data = http_build_query($data);
+      
+      $ch = curl_init();
+      //set the url, number of POST vars, POST data
+      curl_setopt($ch,CURLOPT_URL, $url);
+      curl_setopt($ch,CURLOPT_POST, true);
+      curl_setopt($ch,CURLOPT_POSTFIELDS, $url_data);
+      
+      //So that curl_exec returns the contents of the cURL; rather than echoing it
+      curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
+      
+      //execute post
+      $result = curl_exec($ch);
+      
+      echo $result;
+  }
+  
+  if(isset($_GET['address'])|isset($_GET['magicKey'])) {
+      
+      $url = 'http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find';
+      
+      $data = array('f' => 'pjson',
+          'searchExtent' => '{"xmin":-0.5,"ymin":49.732307856286,"xmax":-1.97117692088061,"ymax":48.4,"spatialReference":{"wkid":84}}',
+          'magicKey' => $_GET['magicKey'],
+          'text' => $_GET['address']
+      );
+      
+      //url-ify the data for the POST
+      $url_data = http_build_query($data);
+      
+//       var_dump($url_data);
+      
+      $ch = curl_init();
+      //set the url, number of POST vars, POST data
+      curl_setopt($ch,CURLOPT_URL, $url);
+      curl_setopt($ch,CURLOPT_POST, true);
+      curl_setopt($ch,CURLOPT_POSTFIELDS, $url_data);
+      
+      //So that curl_exec returns the contents of the cURL; rather than echoing it
+      curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
+      
+      //execute post
+      $result = curl_exec($ch);
+      
+      echo $result;
+  }
+  
+  
+?>

+ 9 - 8
js/app.js

@@ -67,10 +67,11 @@ app.controller('mainController', function($scope, $detection, $http, $mdSidenav,
     spatialReference: { wkid: 84 }
   };
 
-  var suggestURL =
-    'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/suggest?f=pjson&searchExtent=' +
-    JSON.stringify(searchExtent);
-
+//  var suggestURL =
+//    'http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/suggest?f=pjson&searchExtent=' +
+//    JSON.stringify(searchExtent);
+  var suggestURL = 'ajax/geocode.php'
+	  
   var maps = {
     carte: 'http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}',
     satellite: 'http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}'
@@ -935,12 +936,11 @@ app.controller('mainController', function($scope, $detection, $http, $mdSidenav,
   };
 
   $scope.getGeoCode = function(address, magicKey, callback) {
-    var findURL =
-      'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find?f=pjson&searchExtent=' +
-      JSON.stringify(searchExtent);
+    var findURL = 'ajax/geocode.php'
 
     getGeoCodeInProgress = true;
-    var url = findURL + '&text=' + address + (magicKey ? '&magicKey=' + magicKey : ', Basse-Normandie, France');
+    var url = encodeURI(findURL + '?address=' + address + (magicKey ? '&magicKey=' + magicKey : ', Basse-Normandie, France'));
+    
     $.get(url, function(data, status) {
       data = JSON.parse(data);
 
@@ -1422,6 +1422,7 @@ app.controller('mainController', function($scope, $detection, $http, $mdSidenav,
     } catch (e) {
       $scope.suggestionsReady = false;
       i++;
+      
       return $http
         .get(suggestURL, {
           params: {