0

私はjsから始めています。xml を使用してサーバーからデータをフェッチしたい。javascript関数を使用して、リクエストを送信し、xmlでアンサーを取得する方法を知りたいです。POST-Request が必要で、次の形式で xml を送信すると表示されます。

 <?xml version="1.0" encoding="UTF-8"?> 
<ft> 
    <request clientId="123" apiName="api_search_location_stops_nearby" apiVersion="2.0"> 
        <client clientId="123"/> 
        <requestType>api_search_location_stops_nearby</requestType> 
        <outputCoords>WGS84</outputCoords> 
        <fromCoordName>WGS84</fromCoordName> 
        <fromType>coords</fromType> 
        <fromWgs84Lat>48.22</fromWgs84Lat> 
        <fromWgs84Lon>16.39</fromWgs84Lon> 
    </request> 
</ft> 

次にxmlの回答を取得します。そこには 2 つまたは 3 つのノードがあり、私が興味を持っています。そこから先は大したことではありません。

それはウィーンの公共交通機関の奇妙な API に関するものです: http://akirk.github.io/Wiener-Linien-API/ 私は基本的にそれらからデータを取得 (オープン) したいと考えています。

ここ: https://techscreen.tuwien.ac.at/node/794 PHP の解決策を見つけました。

私の試み:

 // Bare bones XML writer - no attributes
function xmlElement(name,content){
    var xml
    if (!content){
        xml = '<' + name + '>' + '</' + name + '>'
    }
    else {
        xml = '<'+ name + '>' + content + '</' + name + '>'
    }
    return xml
}



function sendRequest()
{
    var xmlReq
    xmlReq = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    xmlReq = xmlReq + "<ft>";
    xmlReq = xmlReq + "<request clientId=\"123\" apiName=\"api_get_monitor\" apiVersion=\"2.0\">";
    xmlReq = xmlReq +      "<client clientId=\"123\"/>";
    xmlReq = xmlReq + xmlElement("requestType", "api_get_monitor");
    xmlReq = xmlReq + xmlElement("monitor",
                                 xmlElement("outputCoords", "WGS84") +
                                 xmlElement("type","stop") +
                                 xmlElement("name","60201040") +
                                 xmlElement("year","2013") +
                                 xmlElement("month","10") +
                                 xmlElement("day","3") +
                                 xmlElement("hour","8") +
                                 xmlElement("minute","0") +
                                 xmlElement("line") +
                                 xmlElement("sourceFrom","stoplist") );
    xmlReq = xmlReq + "</request>" + "</ft>";

    text1.text = xmlReq;


    var xhr = new XMLHttpRequest();
    xhr.onload = handleRequest;
    xhr.open("POST", "http://webservice.qando.at/2.0/webservice.ft"); // POST or GET
    xhr.send(xmlReq);
    // xhr.responseXML // this is allways null

}


function handleRequest(answer)
{
    console.log(answer.responseType);
    console.log(answer.responseXML);
}

私の質問の要点: 私のコードでは、GET または POST が必要ですか? リクエストは上記のスタイルに合うように構築されていますか (または、改行が必要か、DOM xml のものに変換する必要がありますか)? 受信のしくみ。私はこれを正しく行っていますか?変数 answer には、答えを含む xml を含める必要がありますか?

このコードはどういうわけか機能していません。xml-string をコンソールに出力すると、上記のようになります (改行なし)。しかし、handleRequest 関数は何も出力しません (まったく呼び出されません)。

前もって感謝します!

4

1 に答える 1