【问题标题】:loading Ajax response data into knockout array将 Ajax 响应数据加载到敲除数组中
【发布时间】:2013-11-17 17:32:18
【问题描述】:

我正在尝试将 AJAX 请求的响应数据加载到可淘汰的可观察数组中。

这是我的 AJAX 请求:

self.loadAssets = function(projectId) { 
    $.ajax({
      url: "/Assets/getJson/"+projectId,
      dataType: 'json',
      success: function(json_data){
        console.log(json_data);
        alert('success');
        $.each(json_data['getassets'], function(){

            viewModel.addAsset(this.type, this.content, 'user-'+this.type, this.width, this.height, this.top, this.left);
        });
      }
    });

};

在inspector中,我可以看到对“/Assets/getJson/id”的请求是成功的(200码) 但是从不调用警报??

我花了一整天的时间查看示例和教程,但我无法弄清楚我做错了什么。

我的 Json 文件如下所示:

{ 
"getassets":[ 
    { 
        "id":1, 
        "type":"Text", 
        "content":"[Your Text]", 
        "width":"200", 
        "height":"100", 
        "top":"0", 
        "left":"0", 
        "selected":true 
    }, 
    { 
        "id":2, 
        "type":"Text", 
        "content":"[Your Text]", 
        "width":"200", 
        "height":"100", 
        "top":"100", 
        "left":"200", 
        "selected":true 
    }, 
    { 
        "id":3, 
        "type":"Text", 
        "content":"[Your Text]", 
        "width":"200", 
        "height":"100", 
        "top":"0", 
        "left":"0", 
        "selected":true 
    }, 
]
}

【问题讨论】:

  • 尝试添加错误处理程序,可能是您收到不正确的json。

标签: javascript jquery ajax json knockout.js


【解决方案1】:

我已经多次看到这种情况发生在内容类型不匹配的情况下。

检查输出的内容类型是否设置为application/json

也添加到您的 ajax 调用中

contentType: "application/json"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多