2012-07-05 10 views
5

मैं Knockout.js का उपयोग कर अपने वेब आवेदन पर निम्न त्रुटि हो रही है के लिए खोज करने के लिए ऑपरेटर 'में' उपयोग नहीं कर सकतेजारी करना, 'लंबाई'

Cannot use 'in' operator to search for 'length' 

मेरे कोड:

$(document).ready(function() { 
     AjaxRequest(); 
    }); 

    function AjaxRequest() { 
     $.post("../../Api/DisabilitiesHandler.ashx?method=get", function (data) { 
      var viewModel = { 
       disabilities: ko.observableArray(data) 
      }; 

      ko.applyBindings(viewModel, document.body); 
     }); 
    } 

<table> 
    <tbody data-bind="template: { name: 'disabilitiesRowTemplate', foreach: disabilities }"></tbody> 
</table> 

<script type="text/html" id="disabilitiesRowTemplate"> 
    <tr> 
     <td>Name: 
      <input data-bind="value: Name" /></td> 
     <td> 
      Active <input type="checkbox" data-bind="checked: Active" /></td> 
    </tr> 
</script> 

और ये मेरे मॉडल

public class Disabilities 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public bool Active { get; set; } 
} 

है और यह वेब सेवा का कोड है

context.Response.ContentType = "application/JSON"; 
DbsaDal.Entities.DBSAEntities db = new DbsaDal.Entities.DBSAEntities(); 
List<DbsaDal.Model.Disabilities> disabilities = DbsaDal.Entities.Disabilities.Get(db); 
context.Response.Write(new JavaScriptSerializer().Serialize(disabilities)); 

क्या करना है इसके बारे में कोई सुझाव? मैं हर जगह वेब पर खोज की है और कुछ भी उपयोगी

अद्यतन 1 नहीं मिल सकता है:

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in [{"Id":1,"Name":"Blind","Active":false},{"Id":2,"Name":"Mute","Active":true}] Knockout.js:92 
+0

क्या आपको जावास्क्रिप्ट में यह त्रुटि मिल रही है? यदि ऐसा है, तो आपके ब्राउज़र डीबगर उस रेखा को इंगित करेंगे जहां त्रुटि हो रही है। क्या आप कोड पोस्ट कर सकते हैं जहां त्रुटि हो रही है? –

+0

पूर्ण त्रुटि संदेश, लाइन नंबर और फ़ाइल – Esailija

+0

पोस्ट करें मैंने अपना प्रश्न अपडेट किया है। त्रुटि Knockout.js फ़ाइल – Armand

उत्तर

5

मैं अपने समस्या दिखाई दी:

disabilities: ko.observableArray(data) 

कोड का यह टुकड़ा किया जाना चाहिए था

disabilities: ko.observableArray(ko.utils.parseJson(data)) 
+4

आप $ .post फ़ंक्शन को भी बता सकते हैं कि आप एक जेसन प्रतिक्रिया की उम्मीद कर रहे हैं और jQuery इसे जावास्क्रिप्ट ऑब्जेक्ट में स्वचालित रूप से पार्स करेगा: '$ .post (" url ", function (data) {}, 'json'); ' http://api.jquery.com/jQuery.post/ –

संबंधित मुद्दे