簡體   English   中英

使用ajax檢索xml響應並顯示結果

[英]using ajax to retrieve an xml response and display results

我知道如何使用$ .ajax方法發布。 我有一個將數據發布到付款API的表單,響應如下所示:

<ns2:GetTransactionsResponseType xmlns:ns2="http://www.paymentgateway.com/schema/api/2.0/service.xsd">
<Timestamp>2012-08-14T17:33:55.213+10:00</Timestamp>
<Version>2.0</Version>
<Status>Success</Status>
<total>0</total>
</ns2:GetTransactionsResponseType>

如您所見,交易總數為0,因此第1步使用$ .ajax進行發布請求,然后成功執行:我要執行$('#results'),html('the total transactions are: '+numberoftransactions); 有什么想法/建議嗎? 謝謝

你可以嘗試這樣的事情

$(function(){

    $.ajax({
         type: "GET",
         url: $uri,
         dataType: "xml",
         async: false,
         contentType: "text/xml; charset=\"utf-8\"",
         complete: function(xmlResponse) {

                // So you can see what was wrong...
                console.log(xmlResponse);
                console.log(xmlResponse.responseText);

              $("#preForXMLResponse").text(xmlResponse.responseText);
         }
    });

});

嘗試在responseText中找到所需的值

有關更多信息,請訪問此鏈接

XMLHttpRequest對象

我想rahul已經回答了您的問題,所以我只想補充一下,如果您要嘗試將數據傳遞到url並相應地返回響應,那么您可以在提出ajax請求時利用data屬性。 假設您嘗試根據用戶配置文件獲取xml響應,因此您必須將用戶ID傳遞給url以獲取准確的響應。 你可以這樣子做

$(function(){

$.ajax({
     type: "GET",
     url: $uri,
     data: $user_id, //this id will be passed with the request to the url as query string
     dataType: "xml",
     async: false,
     contentType: "text/xml; charset=\"utf-8\"",
     complete: function(xmlResponse) {

            // So you can see what was wrong...
            console.log(xmlResponse);
            console.log(xmlResponse.responseText);

          $("#preForXMLResponse").text(xmlResponse.responseText);
     }
});

});

希望這對您正在尋找的東西有所幫助,或者可能是我對您的理解不正確。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM