0

このようなjqueryのwcfサービスからのjsonデータがあります

GetBedTypeList1Result1 は wcf の関数です

{
    "GetBedTypeList1Result":[
        {"Code":23,"CompanyCode":null,"Decode":"1 Class New Born Bed","DivisionCode":0,"LocationCode":0,"admDueDepAmt":0,"bedTypeCode":0,"caseTypeCode":0,"caseTypeDecode":null,"ptnClassCode":0,"ptnClassDecode":null,"rsvDueDepAmt":0},
        {"Code":22,"CompanyCode":null,"Decode":"1st Class Bed","DivisionCode":0,"LocationCode":0,"admDueDepAmt":0,"bedTypeCode":0,"caseTypeCode":0,"caseTypeDecode":null,"ptnClassCode":0,"ptnClassDecode":null,"rsvDueDepAmt":0},
        {"Code":5,"CompanyCode":null,"Decode":"Classique Bed","DivisionCode":0,"LocationCode":0,"admDueDepAmt":0,"bedTypeCode":0,"caseTypeCode":0,"caseTypeDecode":null,"ptnClassCode":0,"ptnClassDecode":null,"rsvDueDepAmt":0}
    ],
    "strErrMsg":"Y",
    "chrErrFlg":"c"
}

以下のようにサービスを呼び出しています

         function CallWcfService() {
         //alert("CallWcfServicexxxx");
         jQuery.ajax
        (
         {

             type: Type,
             url: Url,
             data: parameters,
             contentType: ContentType, // content type sent to server
             dataType: DataType, //Expected data format from server
             cache: "false",
             crossDomain: true,   //Same result if i remove this line
             processdata: ProcessData, //True or False
             success: function (msg) 
             {
                 ServiceSucceeded(msg);
             },
             error: ServiceFailed// When Service call fails
         }
       );
     }



     function callService() 
     {
         DataType = "json";
         Type = "GET";
         var par = 4;
         parameters = null;
         Url = "http://192.168.2.42/CWSERVERWCF/bedtypemasterService.svc/GetBedTypeList?callback=?";
         parameters = "{'strErrMsg':'1'},{'chrErrFlg':'A'},{'pcompanycode':'0'},{'pdiv':'1'},{'ploc':'1'}";
        // alert(parameters);
         ContentType = "application/json; charset=utf-8";
         ProcessData = true;
         //alert("sssssasasadsds");
         CallWcfService();
     }

データをフェッチしようとしていますが、以下の lke を取得できません

   function ServiceSucceeded(result)
      {

        if (DataType == "json")
        {
           var obj =  jQuery.parseJSON(JSON.stringify(JSON.stringify(result)));
           for (var x = 0; x < obj.length; x++) 
            {

            }
         }
     }

obj.length で文字数が来ており、jQuery.parseJSON(result) が機能していません

助けてください

4

2 に答える 2

0

JSON.parse(result)代わりに使用してみてください:var obj = jQuery.parseJSON(JSON.stringify(JSON.stringify(result)));

また、dataType$.ajax 呼び出しで as 'json' を指定したため、応答は既に JSON 形式で、解析は必要ありません。

于 2013-07-11T12:32:42.967 に答える
0

結果が json の場合、この方法で解析する必要はありません。データ型が json に設定されている場合、jQuery.ajax は JavaScript オブジェクトを返します。

したがって、 ServiceSucceeded 関数では、結果変数を直接操作できます。ベッドの種類を反復しようとしている場合は、for ループを次のように変更します。

for (var i = 0; i < result.GetBedTypeList1Result.length; i++) {
  // ...do stuff
  // var bed = result.GetBedTypeList1Result[i]]
}
于 2013-07-11T12:34:25.157 に答える