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

查看:19
本文介绍了使用 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,当我更改频道以包含有关设备和当前正在播放的 url 的一些数据时发送.

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或添加任何其他普通的Web技术,我只需要使用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.

任何熟悉做此类事情的人都可以快速概述如何将帖子发送到已知的收听设备/位置并在其中包含数据?

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),这很容易,它在网络上无处不在.谷歌它或查看此页面了解详情.我在下面提供了一个代码片段.仔细阅读它,特别是关于异步"为真和响应处理程序中的闭包的评论.此外,就 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天全站免登陆