2012-01-09 18 views
6

Recientemente estoy trabajando en teléfonos móviles Nokia usando Qt-Qml. Tengo que hacer una solicitud POST a una URL HTTPS determinada. Estoy usando QML y estoy intentando hacerlo en Javascript sin tener suerte.Https POST/GET con Qml/Qt

¿Alguien tiene una idea al respecto? ¿Es posible hacerlo usando Javascript en QML? ¿Algún consejo sobre cómo hacerlo en QT?

Traté de llamar a una función como esta:

var http = new XMLHttpRequest() 
var url = "myform.xsl_submit"; 
var params = "num=22&num2=333"; 
http.open("POST", url, true); 

//Send the proper header information along with the request 
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
http.setRequestHeader("Content-length", params.length); 
http.setRequestHeader("Connection", "close"); 

http.onreadystatechange = function() {//Call a function when the state changes. 
    if(http.readyState == 4 && http.status == 200) { 
     print("ok"); 
    }else{ 
       print("cannot connect"); 
     } 
} 
http.send(params); 
+1

'XMLHttpRequest.DONE' es fácil de recordar que' 4' , Supongo ... –

Respuesta

4

Su declaración if es erróneo: La función se llama varias veces, pero sólo una vez http.readyState = 4. Entonces, imprime mensajes de error aunque todavía no hay errores.

Primero debe verificar si http.readyState = 4, y luego ver el código de estado.

Aquí es un ejemplo de trabajo mínima:

import QtQuick 1.1 

Rectangle { 
    Component.onCompleted: { 
     var http = new XMLHttpRequest() 
     var url = "http://localhost:8080"; 
     var params = "num=22&num2=333"; 
     http.open("POST", url, true); 

     // Send the proper header information along with the request 
     http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
     http.setRequestHeader("Content-length", params.length); 
     http.setRequestHeader("Connection", "close"); 

     http.onreadystatechange = function() { // Call a function when the state changes. 
        if (http.readyState == 4) { 
         if (http.status == 200) { 
          console.log("ok") 
         } else { 
          console.log("error: " + http.status) 
         } 
        } 
       } 
     http.send(params); 
    } 
} 

creé una pseudo-servidor web local con netcat para probarlo:

% echo -e 'HTTP/1.1 200 OK\n\n' | nc -l 8080 
POST/HTTP/1.1 
Content-Type: application/x-www-form-urlencoded;charset=UTF-8 
Content-Length: 15 
Connection: Keep-Alive 
Accept-Encoding: gzip 
Accept-Language: de-DE,en,* 
User-Agent: Mozilla/5.0 
Host: localhost:8080 

num=22&num2=333 
+0

sí, en realidad pongo "var url =" https: // ..... ";" es solo un ejemplo allí ... – fran

+0

@fran Oh, está bien :) Pero encontré algo diferente, que también puede causar el mensaje de error ... – hiddenbit

+0

¡Sí, tienes razón! gracias de todos modos ... todavía estoy luchando haciendo peticiones ... pero de ninguna manera – fran