如何使用JS脚本在EVOThing应用程序中保存请求响应

时间:2016-08-26 23:51:08

标签: javascript web arduino

这可能是一个愚蠢的问题,但我已经工作了两个星期而没有成功。我使用Arduino101通过蓝牙连接到手机上的EVOThings应用程序。该应用程序从Heroku上托管的Flask服务器请求tweeter信息,其想法是通过蓝牙连接将响应中继到Arduino。但是,虽然响应成功,但我在客户端解析响应时无法进行进一步处理并将其发送到Arduino101。以下是一些代码:



<script>
	// Application object.
	var app = {}
	// Connected device.
	app.device = null;
	app.msg = '';
	
	//Get the message.
	app.getMsg = function()
	{	
		$.ajax({ type: "GET",
			 contentType: "application/json; charset=utf-8",
			 url: 'http://peony-curie.herokuapp.com/getPushNotifications',   
			 async: false,
			 dataType: "json",
			 //navigator.notification.alert("hello", function() {});
			 success : function(text)
			 {
				// navigator.notification.alert("hello", function() {});
				 var res = JSON.stringify(text.data);
				// navigator.notification.alert("hello", function() {});
				 //document.getElementById("try").innerHTML=res;
				 var message = '';
				 for(i = 0; i < res.length; i++){
					message += res[i];
				 }
				 app.msg += message;
				 //navigator.notification.alert("hello", function() {});
				 
			 }
		});
	}
	// Turn on LED red.
	app.ledRed = function()
	{
		app.device && app.device.writeDataArray(new Uint8Array([1]), '19b10001-e8f2-537e-4f6c-d104768a1214');
	}
	
	// Turn on LED green
	app.ledGreen = function()
	{
		navigator.notification.alert(app.msg, function() {});
		app.device && app.device.writeDataArray(new Uint8Array([2]), '19b10001-e8f2-537e-4f6c-d104768a1214');	
	}
		// Turn on LED blue
	app.ledBlue = function()
	{
		app.device && app.device.writeDataArray(new Uint8Array([3]), '19b10001-e8f2-537e-4f6c-d104768a1214');
	}
		// Turn on LED yellow
	app.ledYellow = function()
	{
		app.device && app.device.writeDataArray(new Uint8Array([4]), '19b10001-e8f2-537e-4f6c-d104768a1214');
	}
		// Turn on LED cyan
	app.ledCyan = function()
	{
		app.device && app.device.writeDataArray(new Uint8Array([5]), '19b10001-e8f2-537e-4f6c-d104768a1214');
	}
		// Turn on LED purple
	app.ledPurple = function()
	{
		app.device && app.device.writeDataArray(new Uint8Array([6]), '19b10001-e8f2-537e-4f6c-d104768a1214');
	}
		// Turn on LED white
	app.ledWhite = function()
	{
		app.device && app.device.writeDataArray(new Uint8Array([7]), '19b10001-e8f2-537e-4f6c-d104768a1214');
	}
	
	// Turn off LED.
	app.ledOff = function()
	{
		app.device && app.device.writeDataArray(new Uint8Array([0]), '19b10001-e8f2-537e-4f6c-d104768a1214');
	}
	app.showMessage = function(info)
	{
		document.getElementById('info').innerHTML = info
	};
	// Called when BLE and other native functions are available.
	app.onDeviceReady = function()
	{
		app.showMessage('Touch the connect button to begin.');
	};
	app.connect = function()
	{
		evothings.arduinoble.close();
		app.showMessage('Connecting...');
		evothings.arduinoble.connect(
			'ARDUINO 101-3E4E', // Advertised name of BLE device.
			function(device)
			{
				app.device = device;
				app.showMessage('Connected! Touch buttons to turn LED on/off.');
			},
			function(errorCode)
			{
				app.showMessage('Connect error: ' + errorCode + '.');
			});
	};
	document.addEventListener(
		'deviceready',
		function() { evothings.scriptsLoaded(app.onDeviceReady) },
		false);
	</script>
&#13;
&#13;
&#13;

我所做的是修改EVOThing示例,将Arduino101连接到带有BLE的手机应用程序,方法是添加一个函数来使用AJAX执行请求并将其保存在全局变量中,然后我计划将其发送到Arduino。但是msg永远不会存储在变量中。实际上,即使使用alert函数来调试AJAX帖子中的成功代码块,我什么也得不到。服务器端获得响应并返回OK 200代码,如下所示: Heroku Logs from server side

由于某种原因,来自服务器的消息永远不会保存到app.msg变量中。任何关于为什么这个设置不起作用的帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

可能是您遇到了查看器的https / http安全问题。由于您正在开发的应用程序是在查看器内的cordova webview中通过https加载的,因此不允许请求非https资源。

然而,在查看器中有一个捆绑的插件,无论如何都可以让你穿过并执行http请求,如下所述:https://evothings.com/evothings-secured-now-serving-over-https/

希望有所帮助!