2012-01-09 14 views
6

Ostatnio pracuję nad telefonami Nokia za pomocą Qt-Qml. Muszę wysłać żądanie POST do danego URL-a HTTPS. Używam QML i próbuję zrobić to w JavaScript bez żadnego szczęścia.Https POST/GET z Qml/Qt

Ktoś ma pomysł na ten temat? Można to zrobić za pomocą JavaScript w QML? Dowolna porada, jak zrobić to w QT?

Próbowałem wywołanie funkcji takiego:

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' jest łatwiejsze do zapamiętania niż' 4' , Myślę ... –

Odpowiedz

4

Twój if stwierdzenie jest błędne: Funkcja nazywa się kilka razy, ale tylko raz http.readyState = 4. W związku z tym drukujesz komunikaty o błędach, mimo że nie ma jeszcze błędów.

Najpierw sprawdź, czy http.readyState = 4, a następnie spójrz na kod statusu.

Oto minimalne przykład roboczych:

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); 
    } 
} 

I stworzył lokalny pseudo-serwer WWW z netcata go przetestować:

% 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

tak, właściwie wstawiam "var url =" https: // ..... ";" to tylko przykład tam. – fran

+0

@fran Oh, ok :) Ale znalazłem coś różne, które mogą powodować komunikat o błędzie, zbyt ... – hiddenbit

+0

Tak, masz rację! i tak dziękuję ... wciąż zmagam się z prośbami ... ale nie ma mowy – fran