使用Javascript触发事件发送HTTP Post [英] Sending an HTTP Post using Javascript triggered event

查看:134
本文介绍了使用Javascript触发事件发送HTTP Post的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javascript的新手,我正在研究一种通过IP解码视频的嵌入式系统。

I'm pretty new to javascript and am working on an embedded system which decodes video over IP.

我写了一个小应用程序来设置和更改频道使用javascript并包含一个用于远程控制和事件处理程序的密钥处理程序,这样我可以采取一些操作或在视频停止或网络出现故障时显示消息,但现在我还想设置一个自动HTTP POST,当我发送时更改频道以包含有关设备和当前正在播放的网址的一些数据。

I have written a small app for setting up and changing channels using javascript and included a key handler for remote controls and an event handler so I can take some action or present a message if video stops or the network goes down, but now I also want to set up an automatic HTTP POST that gets sent when I change channel to include some data about the device and the url currently being played.

这是一个运行busybox的小型嵌入式硬件设备,因此我无法使用Ajax或添加任何其他正常的网络技术,我只需要使用Javascript发送由我监控的事件触发的HTTP POST,所以我的第一个目标是能够按下按钮并发送该POST消息,然后计算出何时触发它。

This is a small embedded hardware device running busybox, so I can't use Ajax or add any other normal web technologies, I just need to use Javascript to send a HTTP POST triggered by events I am monitoring, so my first goal is to be able to press a button and send that POST message then work out when to trigger it later.

任何熟悉此类事情的人都可以快速了解如何将帖子发送给已知的听力设备ce / location并在其中包含数据?

Anyone familiar with doing such things that can give me a quick overview of how to send a post to a known listening device/location and include data in it?

非常感谢

推荐答案

如果您的Javascript引擎支持XMLHttpRequest(XHR),这很容易,这在网络上无处不在。谷歌或查看此页面了解详情。我在下面提供了一个代码段。仔细阅读,特别是关于async的评论是真实的,关闭处理程序中的闭包。此外,就Javascript而言,此代码是超轻量级的,我希望它可以在任何当代硬件足迹上正常工作。

This is easy if your Javascript engine supports XMLHttpRequest (XHR), which is ubiquitous on the web. Google it or see this page for details. I've provided a code snippet below. Read it carefully, particularly the comments on "async" being true and closures in response handlers. Also, this code is super lightweight as far as Javascript goes and I would expect it would work fine on just about any contemporary hardware footprint.

var url = "http://www.google.com/";
var method = "POST";
var postData = "Some data";

// You REALLY want shouldBeAsync = true.
// Otherwise, it'll block ALL execution waiting for server response.
var shouldBeAsync = true;

var request = new XMLHttpRequest();

// Before we send anything, we first have to say what we will do when the
// server responds. This seems backwards (say how we'll respond before we send
// the request? huh?), but that's how Javascript works.
// This function attached to the XMLHttpRequest "onload" property specifies how
// the HTTP response will be handled. 
request.onload = function () {

   // Because of javascript's fabulous closure concept, the XMLHttpRequest "request"
   // object declared above is available in this function even though this function
   // executes long after the request is sent and long after this function is
   // instantiated. This fact is CRUCIAL to the workings of XHR in ordinary
   // applications.

   // You can get all kinds of information about the HTTP response.
   var status = request.status; // HTTP response status, e.g., 200 for "200 OK"
   var data = request.responseText; // Returned data, e.g., an HTML document.
}

request.open(method, url, shouldBeAsync);

request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
// Or... request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
// Or... whatever

// Actually sends the request to the server.
request.send(postData);

这篇关于使用Javascript触发事件发送HTTP Post的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆