2013-02-14 15 views
9

Sono piuttosto nuovo su javascript e sto lavorando su un sistema incorporato che decodifica video su IP.Invio di un post HTTP utilizzando l'evento attivato Javascript

Ho scritto una piccola app per l'impostazione e la modifica dei canali utilizzando javascript e incluso un gestore di chiavi per i telecomandi e un gestore di eventi in modo da poter intervenire o presentare un messaggio se il video si interrompe o la rete va giù, ma ora voglio anche impostare un POST HTTP automatico che viene inviato quando cambio canale per includere alcuni dati sul dispositivo e l'url attualmente in riproduzione.

Questo è un piccolo dispositivo hardware embedded che esegue busybox, quindi non posso usare Ajax o aggiungere altre normali tecnologie web, ho solo bisogno di usare Javascript per inviare un POST HTTP innescato da eventi che sto monitorando, quindi il mio primo l'obiettivo è di essere in grado di premere un pulsante e inviare quel messaggio POST, quindi capire quando attivarlo in seguito.

Chiunque abbia familiarità con tali operazioni può darmi una rapida panoramica su come inviare un post a un dispositivo/posizione di ascolto noto e includere i dati in esso contenuti?

Molte grazie

+0

Qual è il tuo motore/runtime Javascript? Hai il supporto XMLHttpRequest? I due principali ambienti javascript che conosco sono Javascript nel browser e node.js. Il primo utilizza XMLHttpRequest per inviare messaggi HTTP. In seguito utilizza le librerie di supporto special.js.js. Ciascuno fornisce un'API di trasmissione molto semplice. –

+0

Ciao Will, Sì, abbiamo il supporto XMLHttpRequest. –

risposta

24

Questo è facile se il motore Javascript supporta XMLHttpRequest (XHR), che è onnipresente sul web. Google o vedi this page per i dettagli. Di seguito ho fornito uno snippet di codice. Leggilo attentamente, in particolare i commenti su "async" sono veri e chiusure nei gestori di risposta. Inoltre, questo codice è super leggero per quanto riguarda Javascript e mi aspetterei che funzionerebbe bene praticamente su qualsiasi impronta hardware contemporanea.

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); 
+0

Grazie - questo aiuta molto. –

+3

Sto usando node.js e questo ha funzionato per me dopo aver aggiunto la seguente riga prima di var request = new XMLHttpRequest(); var XMLHttpRequest = require ("xmlhttprequest"). XMLHttpRequest; potresti anche voler installare XMLHttpRequest $ npm installare xmlhttprequest – saad

Problemi correlati