Javascript 尝试将responseJSON存储在变量结果中;“未定义”;错误

Javascript 尝试将responseJSON存储在变量结果中;“未定义”;错误,javascript,json,rest,backbone.js,Javascript,Json,Rest,Backbone.js,在我的REST应用程序中,我尝试使用HTTPGET方法获取数据。作为响应,我得到一个对象,其responseText和responseJSON字段如下: Object {readyState: 1} abort: ( statusText ) { always: () { complete: () { done: () { error: () { fail: () { getAllResponseHeaders: () { getResponseHeader: ( key ) { overrid

在我的REST应用程序中,我尝试使用HTTPGET方法获取数据。作为响应,我得到一个对象,其responseText和responseJSON字段如下:

Object {readyState: 1}
abort: ( statusText ) {
always: () {
complete: () {
done: () {
error: () {
fail: () {
getAllResponseHeaders: () {
getResponseHeader: ( key ) {
overrideMimeType: ( type ) {
pipe: ( /* fnDone, fnFail, fnProgress */ ) {
progress: () {
promise: ( obj ) {
readyState: 4
responseJSON: Array[3]
responseText: "[{"id":1,"name":"Standard"},{"id":2,"name":"Small (32 cm)"},{"id":3,"name":"Big (42 cm)"}]"
setRequestHeader: ( name, value ) {
state: () {
status: 200
statusCode: ( map ) {
statusText: "OK"success: () {then: ( /* fnDone, fnFail, fnProgress */ ) {
__proto__: Object 
当我尝试在变量中存储responseText或responseJSON时,会在控制台中导致“未定义”错误。下面是一段代码:

define([
    'backbone'
],
    function (Backbone) {
        'use strict';

        return Backbone.Collection.extend({
            initialize: function () {
                this.sizes = this.fetch();
            },

            url: 'http://localhost:8080/sizes',

            getContext: function () {
                var sizelist = this.sizes.responseJSON;
                return sizelist;
            }
        });
    });

当我尝试执行console.log()时,变量“sizelist”是未定义的,尽管“this.sizelist”是上面介绍的对象。问题是如何获取存储在responseJSON或responseText字段中的数据。非常感谢您的帮助。

您需要等待查询完成才能获取

       function () {
            var self = this;
            this.fetch({
                success: function(response) {
                    // store your response from here
                    self.sizes = response;
                }
            });
        }

您需要等待查询完成才能获取

       function () {
            var self = this;
            this.fetch({
                success: function(response) {
                    // store your response from here
                    self.sizes = response;
                }
            });
        }

为什么在
this.size.responseJSON
中有
size
可能重复?我想您应该只访问
responseJSON
属性(即
this.responseJSON
)。为什么在
this.size.responseJSON
中有
size
?我想您应该只访问
responseJSON
属性(即
this.responseJSON
)。