Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js parseJSON(响应)为空_Backbone.js - Fatal编程技术网

Backbone.js parseJSON(响应)为空

Backbone.js parseJSON(响应)为空,backbone.js,Backbone.js,我有一个主干模型,它正在发出一个成功的服务器请求。回调使用主干的触发器方法来触发关联视图中的事件,并将来自服务器请求的解析json响应作为触发器方法的第二个参数传递,如主干文档中所述。在视图中,事件触发render方法,但响应对象在视图中为null。它抛出了这个错误 Uncaught TypeError: Cannot read property 'word' of null 有人能看出我可能做错了什么吗 var json = $.parseJSON(response); console

我有一个主干模型,它正在发出一个成功的服务器请求。回调使用主干的触发器方法来触发关联视图中的事件,并将来自服务器请求的解析json响应作为触发器方法的第二个参数传递,如主干文档中所述。在视图中,事件触发render方法,但响应对象在视图中为null。它抛出了这个错误

Uncaught TypeError: Cannot read property 'word' of null 
有人能看出我可能做错了什么吗

 var json = $.parseJSON(response);
 console.log(json)
来自该型号的服务器请求

  new: function() {
      var _this = this;
       console.log("new function of model"); 

      $.ajax({
        url: "/gamestart",
        type: "GET",
        success: function(response) {
          console.log(response);
          var json = $.parseJSON(response);

          _this.set({lost: false});
          _this.set({win: false});
          _this.trigger("gameStartedEvent", json);
        }
      })
    },
render: function(response) {
      $("#hint").show();
      console.log(response); 
      var html = this.template({characters: response.word});
      this.el.hide();
      this.el.html(html).show();
    },
视图的初始值设定项方法中触发render方法以呈现响应的事件

this.model.on("gameStartedEvent", this.render, this);
Object {word: Array[6]}
word: Array[6]
__proto__: Object
响应为null的呈现方法

  new: function() {
      var _this = this;
       console.log("new function of model"); 

      $.ajax({
        url: "/gamestart",
        type: "GET",
        success: function(response) {
          console.log(response);
          var json = $.parseJSON(response);

          _this.set({lost: false});
          _this.set({win: false});
          _this.trigger("gameStartedEvent", json);
        }
      })
    },
render: function(response) {
      $("#hint").show();
      console.log(response); 
      var html = this.template({characters: response.word});
      this.el.hide();
      this.el.html(html).show();
    },
注意,如果重要的话,视图是用模型实例化的

var word_view = new WordView({model: game})
更新

this.model.on("gameStartedEvent", this.render, this);
Object {word: Array[6]}
word: Array[6]
__proto__: Object
实际上错误就发生在这里。响应已成功返回,但我分析错误。当我检查控制台时,变量json为null。你能看出我做错了什么吗

 var json = $.parseJSON(response);
 console.log(json)
响应

this.model.on("gameStartedEvent", this.render, this);
Object {word: Array[6]}
word: Array[6]
__proto__: Object

实际上不需要在响应对象上调用parseJSON。我可以直接从response对象获取word属性,因此我只将response对象作为第二个参数传递给trigger,并在视图中调用
response.word

 $.ajax({
        url: "/gamestart",
        type: "GET",
        success: function(response) {
          console.log(response.word);
          var json = $.parseJSON(response);    ####unnecessary to parseJSON here
          console.log(json);

          _this.set({lost: false});
          _this.set({win: false});
          _this.trigger("gameStartedEvent", response);
        }
      })
    },

在尝试
$.parseJSON
之前,
响应中有什么内容?@muistooshort我添加到操作末尾的对象。出于某种原因,我不必解析它来调用word。我只是将此代码从sintra应用程序改编为rails应用程序。为sinatra制作的人解析了响应对象。如果内容类型标题正确,则应自动解析JSON,可能是sinatra未正确设置。大概,将非字符串馈送到
$.parseJSON
会给您带来一个
null
,以解决您的问题。