Take the 2-minute tour ×

this puzzles me. it must be something small i'm not seeing. i'm trying to load a very simple observableArray in knockout with an ajax call.


// we bind the array to the view model property with an empty array.
var data = [];   
var viewModel = {
    vendors: ko.observableArray(data)

$(function () {
    // on this click event, we popular the observable array
    $('#load').click(function () {
        // WORKS. Html is updated appropriately.
        viewModel.vendors([{ "Id": "01" },{ "Id": "02" },{ "Id": "03" }]);

        // DOES NOT WORK. Fiddler2 shows the same exact json string come back 
        // as in the example above, and the success function is being called.
            url: '/vendors/10',
            dataType: 'json',
            success: function (data) {


<button id="load">Load</button>
<ul data-bind="template: { foreach: vendors }">
    <li><span data-bind="text: Id"></span></li>

Question: Why does the successful ajax call, who's data variable value matches byte-for-byte the hard typed value, not trigger the html refresh?

share|improve this question

4 Answers 4

up vote 6 down vote accepted

There is no reason this would not work fine. As this demonstrates.


I would check that the ajax post is actually returning json data and that that json is an array and that it's being parsed correctly.

I had to tweak the ajax call to get the fiddle ajax handlers to work correctly.

Nothing more I can think of.

Hope this helps.

share|improve this answer
thanks for validating my sanity... i'll look closer again. maybe MVC suck in a little wrapper or something... –  one.beat.consumer Mar 16 '12 at 5:51
Yeh. Firebug the request response, verify you are getting json and then verify jquery is parsing it to an object. –  madcapnmckay Mar 16 '12 at 5:55
it is json for certain... the C# code looks like return Json(list, JsonResponseBehavior.AllowGet); where list is an ICollection<Vendor>` so I know it is JSON. Add to that, that Fiddler2 shows the data in its JSON view correctly... it's in the javascript somewhere. i'll post again tomorrow with more info when im at work –  one.beat.consumer Mar 16 '12 at 5:58

This is bug I think, Knockout's sample is working when we use it with wrapper class:

public class ResultWrapper
    public Title {get;set;}
    public List<Result> {get;set;}


But if we return Results directly there is no way to bind it. (without extra applyBindings!)

share|improve this answer
var self=this;
//var self first line in model

            url: ",
            dataType: "json",
            contentType: 'application/json',
            type: "POST",
            data: JSON.stringify({ }),
            processdata: true,

            beforeSend: function () {

            error: function (xhr, textStatus, errorThrown) {

            success: function (data) {

                if (data.result!= '') {

                } else {


Use self.vendors not this viewModel.vendors

share|improve this answer

I am way too late on this one as I found myself stuck in this situation just recently. We can use a simple Javascript util function as a work-around.

Instead of viewModel.vendors(data);, we can use


It worked for me.

share|improve this answer

Your Answer


