簡體   English   中英

在JavaScript中,如何遍歷來自asmx Web服務的數據? (由JavaScriptSerializer序列化)

[英]in JavaScript how can I loop through data from an asmx web service? (serialized by a JavaScriptSerializer)

我的.asmx Web服務返回的文件路徑和文件名對如下:

[{"thumb":"path/image01_thumb.jpg","image":"path/image01.jpg"},{"thumb":"path/image01_thumb.jpg","image":"path/image01.jpg"}.....etc ] 

我試圖遍歷響應並獲取每個元素的屬性thumbimage ,但是我在下面的所有嘗試都使我失敗了。

$.ajax({
   type: "POST",
   url: "mywebservice",
   data: "{'startpath':'somepath'}",
   contentType: "application/json; charset=utf-8",
   dataType: "json",
   async: false,
   success: function (msg) {
      $.each (msg, function (m) {
         console.log("alert 3   " + m.thumb);   // output: undefined
         console.log("alert 4   " + m.image);   // output: undefined
   });
}

進一步調試...

console.log("alert 1   " + msg); //output: [object object]
console.log("alert 2   " + msg.d); //output: the entire response 

如何遍歷響應並獲取上述值。 在chrome和IE 9中,我得到了相同的結果。使用Jquery 1.3.2卡住了,因為我使用的是galleriffic庫。

您有一個JSON字符串,將其解析為一個對象。

var anObject = JSON.parse(msg);

然后,您可以引用項目及其屬性

anObject[i].thumb

暫無
暫無

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

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