我用jquery,Post,返回數據,數據是Json.d的數據" [{"id" : "1"},{"id" : "2"},{"id" : "3"}]" ,
將他以列表形式顯示,我失敗啦。
但是,如果 var data=[{"id" : "1"},{"id" : "2"},{"id" : "3"}],顯示則成功。
請問,我是少那一步呢?
附:代碼:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
url:"../WebService/Shop.asmx/GetProductByType",
data:"{id:\""+"1" + "\"}",
success: function(data,type){
var json = null;
try{
var test= [{"id" : "1"},{"id" : "2"},{"id" : "3"}];
test=eval (data);
//var JSONObject a = new JSONObject(data);
var str=" <table>";
for (var i=0;i <=test.length-1 ;i++)
{
str+=" <tr> <td>"+"a" + test[i].id +" </td> </tr>"; //循环生成tr标记
}
/*
$.each
(
data.d, function(i, n)
{
//alert (i);
str+=" <tr> <td>"+"a" + n.id +" </td> </tr>"; //循环生成tr标记
}
);
*/
str+=" </table>";
$("#ShopList").html(str );
// $("#sPrice").html(data.d );
}catch(e){
alert("Parse Error");
return;
}
//alert(json.d);
},
error: function(x, e) {
$("#ShopList").html(x.responseText );
},
complete: function(x) {
// $("#SendMail").attr("disabled", false );
}
}); |