Javascript 向客户端发送JSON的正确方式是什么?
我想为Pebble开发一个应用程序。这个应用程序将告诉你从一个你在选项中设置的地方到另一个考虑到交通堵塞等因素的地方需要多长时间。 为了实现这一点,我需要创建一个返回JSON的页面。Pebble使用如下代码检索信息:Javascript 向客户端发送JSON的正确方式是什么?,javascript,json,pebble-watch,Javascript,Json,Pebble Watch,我想为Pebble开发一个应用程序。这个应用程序将告诉你从一个你在选项中设置的地方到另一个考虑到交通堵塞等因素的地方需要多长时间。 为了实现这一点,我需要创建一个返回JSON的页面。Pebble使用如下代码检索信息: var cityName = 'London'; var URL = 'http://api.openweathermap.org/data/2.5/weather?q=' + cityName; ajax( { url: URL, type: 'json' }, fu
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);
}
);
我用js脚本创建了一个小页面,从Yandex API获取所需信息:
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);
}
);
}
正如您所看到的,我最终可以得到一个JSON字符串。但是,当发出具有正确参数的请求时,如何将其发送给客户端?我最终使用phantomjs并在我的php页面上执行此js脚本