[英]Ajax http request from Pebble to Raspberry Pi Python server fails
作为Pebble / Ajax / Java Script / Python新手,我正在研究非常基本的代码,以便在Pebble手表上接收Raspberry Pi传感器的读数:
我正在执行从Pebble到Raspberry Pi Python服务器的简单Ajax请求,但无法获得任何响应(下面是“客户端/服务器”代码)。
但是,当我打开Web浏览器并输入Raspberry Pi(例如)的本地URL时,Raspberry服务器响应成功。 192.168.1.80:9999:{“ Pi响应”:“ Hello World!}”
当我将本地URL从192.168.1.80更改为远程网页上的一个非常简单的php-script( http://if.christianbirch.dk/helloworld.php )时,Pebble响应也会成功:[PHONE] pebble-app .js:?:{“ Web响应”:“ Hello World!”}
我已经在许多论坛中搜索了解决方案,但是没有运气。 但是,我怀疑本地URL或端口号在Ajax脚本中未正确定义-或Raspberry Pi Python脚本可能缺少标头语句。
任何经验重新。 这个问题? 先感谢您!
Pebble JS脚本:
var ajax = require('ajax');
ajax(
{
url: 'http://192.168.1.80', // *** Valid reponse is received when changing this URL to http://if.christianbirch.dk/helloworld.php ***
method: 'get',
type: 'json',
port:'9999'
},
function(data) {
// Success!
console.log(JSON.stringify(data));
},
function(error) {
// Failure!
console.log('No response!');
}
);
Python服务器脚本(从网络浏览器调用时收到有效响应) https://docs.python.org/2/library/socketserver.html
import SocketServer
class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
self.reqdata = self.request.recv(1024).strip()
print self.reqdata
self.send('{"Pi Reponse":"Hello world!"}')
def send(self.content):
self.request.sendall('HTTP/1.0 200 OK\n\n{}.format(content))
if __name__ == "__main__":
HOST, PORT = "0.0.0.0", 9999
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
server.serve_forever()
您似乎已经发现,在CloudPebble中,基于Web的仿真器没有本地网络访问权限。 要将CloudPebble与本地网络应用程序开发一起使用,您需要将Phone设置为编译目标。 我也遇到了这个问题,同时还针对我的Pi进行了开发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.