簡體   English   中英

在sessionStorage中存儲和檢索Ajax xml響應

[英]storing and retrieving ajax xml response in sessionStorage

我從ajax獲取XML響應,然后嘗試將其存儲在sessionStorage中,但無法檢索它。

$.ajax({
        url: '/webservice.svc/getProfile',
        data: { "memberId": $(authenticateResponse).find("memberId").text() },
        method: "GET",
        async: false,
        success: function (d) {
            sessionStorage.clear();
            sessionStorage.setItem('ALMS', d);
        }
    });

當我嘗試檢索

console.dirxml(sessionStorage.ALMS)
[object XMLDocument]
console.dirxml($(sessionStorage.ALMS)) 

我做錯了!

您遇到的問題是sessionStorage僅可容納字符串。 因此,使用toString()強制了XMLDocument,因此您看到的是值。

要解決此問題,您需要在保存之前手動序列化XMLDocument。 嘗試這個:

$.ajax({
  url: '/webservice.svc/getProfile',
  data: { 
    memberId: $(authenticateResponse).find("memberId").text() 
  },
  method: "GET",
  success: function (d) {
    sessionStorage.clear();
    sessionStorage.setItem('ALMS', new XMLSerializer().serializeToString(d));
  }
});

還要注意,我刪除了async: false因為這是非常糟糕的做法。

暫無
暫無

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

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