2012-01-09 17 views
6

最近、私はQt-Qmlを使ってNokiaの携帯電話に取り組んでいます。私は与えられたHTTPS URLにPOST要求をしなければなりません。 私はQMLを使用しています。私はJavascriptでこれをやってみようとしています。Https POST/GET with Qml/Qt

誰でも知っていますか?それはQMLのJavascriptを使って行うことができますか? QTでそれを作る方法に関するアドバイスはありますか?

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'は'よりも覚えやすいです4' ... –

答えて

4

あなたif文が間違っている:

は、私はこのような関数を呼び出してみました関数が複数回呼び出されたが、一度だけ http.readyState = 4。したがって、まだエラーはありませんが、エラーメッセージを出力します。

まず、http.readyState = 4を確認してから、ステータスコードを確認してください。ここで

は最小限の作業例です。

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

私はそれをテストするためのnetcatとローカル擬似ウェブサーバを作成しました:

% 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

はい、実際には を入れます "var url =" https:// ..... ";"それはそこの例です.. – fran

+0

@fran Oh、ok :)しかし、私は何かを見つけました異なる、エラーメッセージを引き起こす可能性もあります... – hiddenbit

+0

はい、あなたは正しいです!とにかくありがとうございます。依頼をやってもまだ苦労しています。 – fran