简体   繁体   English

如何从 yaler 获得 json 响应

[英]How to get a json response from yaler

I create an account with yaler, to comunicate with my arduino yun.我在 yaler 创建了一个帐户,以与我的 arduino yun 进行交流。 It works fine, and i'm able to switch on and off my leds.它工作正常,我可以打开和关闭我的 LED。 Then i created a web page, with a button that calls an ajax function with GET method to yaler (yaler web server accept REST style on the URL)然后我创建了一个网页,带有一个按钮,该按钮使用 GET 方法调用 ajax 函数到 yaler(yaler web 服务器接受 URL 上的 REST 样式)

$.ajax({
   url: "http://RELAY_DOMAIN.try.yaler.net/arduino/digital/13/1",
   dataType: "json",
   success: function(msg){
      var jsonStr = msg;
    },
   error: function(err){
       alert(err.responseText);
   }

});

This code seem to work fine, infact the led switches off and on, but i expect a json response in success function (msg) like this:这段代码似乎工作正常,实际上 LED 灯会关闭和打开,但我希望在成功函数 (msg) 中有一个 json 响应,如下所示:

{
"command":"digital",
"pin":13,
"value":1,
"action":"write"
}

But i get an error (error function).但是我收到一个错误(错误函数)。 I also tried to alert the err.responseText, but it is undefined....我也试图提醒 err.responseText,但它是未定义的....

How could i solve the issue?我怎么能解决这个问题? Any suggestions???有什么建议??? Thanks in advance....提前致谢....

If the Web page containing the above Ajax request is served from a different origin, you'll have to work around the same origin policy of your Web browser.如果包含上述 Ajax 请求的 Web 页面是从不同的源提供的,则您必须解决 Web 浏览器的相同源策略。

There are two ways to do this (based on http://forum.arduino.cc/index.php?topic=304804 ):有两种方法可以做到这一点(基于http://forum.arduino.cc/index.php?topic=304804 ):

  • CORS , ie adding the header Access-Control-Allow-Origin: * to the Yun Web service CORS ,即添加头Access-Control-Allow-Origin: *到 Yun Web 服务
  • JSONP , ie getting the Yun to serve an additional JS function if requested by the Ajax call with a query parameter ?callback=? JSONP ,即如果 Ajax 调用请求带有查询参数?callback=?

CORS can probably be configured in the OpenWRT part of the Yun, while JSONP could be added to the Brige.ino code (which you seem to be using). CORS 可能可以在 Yun 的 OpenWRT 部分配置,而 JSONP 可以添加到 Brige.ino 代码(您似乎正在使用)。

I had the same problem.我有同样的问题。 I used JSONP to solve it.我用 JSONP 来解决它。 JSONP is JSON with padding. JSONP 是带有填充的 JSON。 Basically means you send the JSON data with a sort of wrapper.基本上意味着您使用某种包装器发送 JSON 数据。 Instead of just the data you have to send a Java Script function and this is allowed by the internet.您必须发送 Java Script 函数而不仅仅是数据,这是互联网允许的。

So instead of your response being :所以,而不是你的回应是:

{"command":"digital","pin":13,"value":0,"action":"write"}

It should be:它应该是:

showResult({command:"analog",pin:13,value:0,action:"write"});

I changed the yunYaler.ino to do this.我改变了 yunYaler.ino 来做到这一点。

So for the html :所以对于 html :

 var url = 'http://try.yaler.net/realy-domain/analog/13/210'; $.ajax({ type: 'GET', url: url, async: false, jsonpCallback: 'showResult', contentType: "application/json", dataType: 'jsonp', success: function(json) { console.dir(json.action); }, error: function(e) { console.log(e.message); } }); }; function showResult(show) { var str = "command = "+show.command;// you can do the others the same way. alert (str); }

My JSON is wrapped with a showResult() so its made JSONP and its the function I called in the callback.我的 JSON 用 showResult() 包装,因此它制作了 JSONP 以及我在回调中调用的函数。

Hope this helps.希望这可以帮助。 If CORS worked for you.如果 CORS 为您工作。 Could you please put up how it worked here.你能不能把它在这里是如何工作的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM