M
M
morfo2015-09-22 13:21:26
JavaScript
morfo, 2015-09-22 13:21:26

How to send JSON received as a result of the js script to the user's request?

I am developing an app for Pebble. Two addresses are passed as parameters, and at any time from the clock you can see the travel time, taking into account traffic jams. Pebble can receive information in the following way:

var cityName = 'London';
var URL = 'http://api.openweathermap.org/data/2.5/weather?q=' + cityName;

ajax(
 {
  url: URL,
  type: 'json'
},
function(data) {
  // Success!
  console.log('Successfully fetched weather data!');
},
function(error) {
  // Failure!
  console.log('Failed fetching weather data: ' + error);
}
);

I wrote a small script:
var route;
      ymaps.ready(init);
var myMap;

function init(){    


  function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
  }   

        var time = 0;
        var home = getParameterByName("h");
        var work = getParameterByName("w");
      ymaps.route([home, work],{avoidTrafficJams: true}).then(
            function (router) {
                route=router;
                time = ((route.getTime())/60).toFixed(2);

               var info = new Object;
                info["home"] = home;
                info["work"] = work;
                info["time"] = ~~time+"m"+~~((time%1)*60)+"s";
                JSON.stringify(info);
            },
            function (error) {
                alert('Возникла ошибка: ' + error.message);
            }
            );    
}

It turns out that I have a JSON string that I generate on the go, but I can't figure out how to properly send it to clients.
Perhaps I fundamentally misunderstand something, I'm not very good at programming. Thank you in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Spiridonov, 2015-09-22
@customtema

<?
header("Content-Type: text/plain");
echo json_encode($output);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question