简体   繁体   English

从Pebble到Raspberry Pi Python服务器的Ajax http请求失败

[英]Ajax http request from Pebble to Raspberry Pi Python server fails

As a Pebble/Ajax/Java Script/Python newbie I am working on the very basic code in order to receive Raspberry Pi sensor readings on the Pebble watch: 作为Pebble / Ajax / Java Script / Python新手,我正在研究非常基本的代码,以便在Pebble手表上接收Raspberry Pi传感器的读数:

I am executing a simple Ajax request from Pebble to a Raspberry Pi Python server but fail to get a any reponse (Client/Server code is below). 我正在执行从Pebble到Raspberry Pi Python服务器的简单Ajax请求,但无法获得任何响应(下面是“客户端/服务器”代码)。

However, the Raspberry server is responding sucessfully when I open a web browser and and enter the local url to the Raspberry Pi, ex. 但是,当我打开Web浏览器并输入Raspberry Pi(例如)的本地URL时,Raspberry服务器响应成功。 192.168.1.80:9999: {"Pi response":"Hello World!}" 192.168.1.80:9999:{“ Pi响应”:“ Hello World!}”

Also the Pebble reponse is successful when I change the local URL from 192.168.1.80 to a very simple php-script on a remote web page ( http://if.christianbirch.dk/helloworld.php ): [PHONE] pebble-app.js:?: {"Web response":"Hello World!"} 当我将本地URL从192.168.1.80更改为远程网页上的一个非常简单的php-script( http://if.christianbirch.dk/helloworld.php )时,Pebble响应也会成功:[PHONE] pebble-app .js:?:{“ Web响应”:“ Hello World!”}

I've searched through many forums for a solution but without luck. 我已经在许多论坛中搜索了解决方案,但是没有运气。 However, I suspect that either the local URL or port number is somehow incorrectly defined in the Ajax script - or the Raspberry Pi Python script could be missing a header sentence. 但是,我怀疑本地URL或端口号在Ajax脚本中未正确定义-或Raspberry Pi Python脚本可能缺少标头语句。

Any experience re. 任何经验重新。 this issue? 这个问题? Thank you in advance! 先感谢您!


The Pebble JS script: 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!');
}
);

The Python server script (Valid reponse received when called from web browser) https://docs.python.org/2/library/socketserver.html 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() 

As you seem to have figured out, in CloudPebble, the web-based emulator does not have local network access. 您似乎已经发现,在CloudPebble中,基于Web的仿真器没有本地网络访问权限。 To use CloudPebble along with local network app development, you need to set your Phone to be the compilation target. 要将CloudPebble与本地网络应用程序开发一起使用,您需要将Phone设置为编译目标。 I ran into this as well while also developing against my Pi. 我也遇到了这个问题,同时还针对我的Pi进行了开发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pebble:JavaScript将数据从服务器发送到Pebble - Pebble: JavaScript Send Data from Server to Pebble 安全地将数据从 Raspberry Pi 发送到 Apache 服务器 - Securely send data from Raspberry Pi to Apache Server 获取存储图像时,来自Raspberry Pi服务器的响应中断 - Broken response from Raspberry Pi server when GETting stored images 是否可以确定对服务器的 HTTP 请求是否源自 Ajax,或单击链接 - is it possible to determine if an HTTP request to the server originated from Ajax, or clicking a link 通过setInterval进行的jQuery .ajax XHR请求在Raspberry PI Chromium上无法正常工作 - jQuery .ajax XHR request via setInterval doesn't work properly on Raspberry PI Chromium Raspberry Pi Fask 服务器:使用 AJAX 和损坏的 id 搜索在文件夹中找不到图像 - Raspberry Pi Fask Server: Cannot find images in folder using AJAX AND broken id search 通过Pebble.js获取AJAX请求时出错 - Error when getting an AJAX request with Pebble.js 发送ajax请求到Netty HTTP服务器 - Send ajax request to Netty HTTP server raspberry上的node.js服务器 - node.js server on raspberry pi Express服务器已启用CORS,尽管在从Angular执行aync HTTP请求时仍然失败 - Express server has CORS enabled, though it still fails when performing aync HTTP request from Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM