Hey Romain, Leaflet-Routing-Machine is a good reference for seeing how to query the OSRM route with javascript from a browser:
https://github.com/perliedman/leaflet-routing-machine/blob/master/src/L.Routing.OSRM.js They use corslite [1] to do the json querry. [1] https://github.com/perliedman/leaflet-routing-machine/blob/master/src/L.Routing.OSRM.js#L55 Good luck, Patrick On Thu, Feb 12, 2015 at 10:46 AM, Romain NKONGO <romain.rn...@gmail.com> wrote: > Hello to all the OSRM community. > > I'm a french student who works on a university project, along with my > supervisor and uses the open source project OSRM to achieve it. > > At some point, I have to run some tests (like thousands of tests) to > extract some values from the JSON outputs (e.g. the total time). Now our > problem is we tried several ways to do this, by sending HTTP GET requests > to the running server and treat the returned response as JSON. But we tried > with Javascript, jQuery and AngularJS, with functions like $resource.get, > jQuery.getJSON $http.get or $http.jsonp and we never managed to get the > returned response in a way that we could use it for further treatment in > our script. Actually, we were able to send the requests to the server but > the response is returned in the fom of an URL link which contains the JSON > output in its body, not in the form of a variable in our Javascript that we > could manipulate. > > As a matter of fact, we have two issues for this : > - some of the functions we used (which are based on jsonp) added a > callback parameter to the sent URL, so the request became invalid with > 'Query malformed at the position' errors > - I've seen in the OSRM documentation that the JSON output is encoded with > the Google polyline algorithm so it could be an invalid JSON for Javascript > (we also observed errors like SyntaxError: JSON.parse: unexpected end of > data at line 1 column 1 of the JSON data > > We essentially want to use the viaroute service, so our URL is something > like that : "htttp://localhost:5000/viaroute?loc=a,b&loc=c,d" with a,b, c > and d random float numbers. > > Has anyone ever tried to run some customized tests on OSRM, with any Web > language, and if so can he give me some tricks of how he succeeded to get > the JSON ouptut where he wanted? > > To be clearer of how I proceeded in AngularJS, here the code of my app.js > : > var app = angular.module('clientOSRM', ['ngResource']); > > app.controller('appController', ['$scope','$http', 'appServices', function > ($scope,$http, appServices) { > //Initialisation de notre input, sa valeur sera stockée en instantané > dans cette variable (ng-model) > $scope.valueInput1 = "47.3654647,0.6822917"; > $scope.valueInput2 = "47.3905003,0.6920979"; > > > var getItineraryParams = function () { > return { > start: $scope.valueInput1, > end: $scope.valueInput2 > //start: "47.3654647,0.6822917", > //end: "47.3905003,0.6920979" > //start: "a,b", > //end: "c,d" > } > }; > > //Une fonction accessible que dans ce controleur (mot clé var) > var getItinerary = function () { > appServices.osrm.get(getItineraryParams(), > function (itinerary) { > alert("Succes : "+typeof itinerary); > console.log(itinerary); > }, > function (error) { > alert("erreur : "+typeof error+"; status : "+error.status); > console.log(error); > } > ); > /*$http.jsonp(" > http://localhost:5000/viaroute?loc=47.3654647,0.6822917&loc=47.3905003,0.6920979&alt=false&geometry=false&output=json > ") > .success(function(data, status, headers, config) { > console.log(data); > }). > error(function(data, status, headers, config) { > console.log(data); > });*/ > > }; > > //Une fonction accessible depuis la vue > //Fonction appelée à l'envoi du formulaire (balise ng-submit dans > <form>) > $scope.submit = function () { > //On peut récupérer la valeur de notre input ! > //console.log($scope.valueInput1); > getItinerary(); > }; > }]); > > app.run(function ($rootScope) { > > $rootScope.safeApply = function (fn) { > var phase = $rootScope.$$phase; > if (phase === '$apply' || phase === '$digest') { > if (fn && (typeof(fn) === 'function')) { > fn(); > } > } else { > this.$apply(fn); > } > }; > > }); > > and also the appServices.js code : > app.factory('appServices', function ($resource) { > > return { > osrm: $resource("", {}, { > 'get': { > method: 'GET', > params: {start: '@start', end: '@end'}, > isArray:true, > url: " > http://localhost:5000/viaroute?loc=:start&loc=:end&alt=false&geometry=false > " > } > }) > }; > }); > > In the getItinerary function in app.js, the error function (the 3rd > parameter) is always triggered even with a HTTP status code '200 OK' of the > request. > > Any ideas where it could go wrong? > > Thanks in advance. > > > > _______________________________________________ > OSRM-talk mailing list > OSRM-talk@openstreetmap.org > https://lists.openstreetmap.org/listinfo/osrm-talk > >
_______________________________________________ OSRM-talk mailing list OSRM-talk@openstreetmap.org https://lists.openstreetmap.org/listinfo/osrm-talk