骨干模型仅返回和对象,或JSON {readyState:1} [英] backbone model is only returning and object, or JSON {readyState : 1}

查看:89
本文介绍了骨干模型仅返回和对象,或JSON {readyState:1}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在学习骨干知识,并同时构建我的第一个应用程序,因此,任何使球再次滚滚的帮助都会很棒.

I am still learning backbone and building my first app at the same time, so any help to get the ball rolling again would be awesome.

首先,我要做的就是从骨干那里得到正确的响应.我100%确定服务器向我发送了纯JSON,因为我可以通过URL在浏览器中对其进行检查.网址与我的代码中的urlRoot相同,只是保存在变量中.

First, all i am trying to do it is get the proper response from the backbone. I am 100% sure that the server is sending me pure JSON, as I can check it in the browser via a URL. Same url as the urlRoot in my code, its just saved in a var.

我以前工作过这种 ,但是我通过一个集合获得了JSON响应(看起来还可以),后来我发现这不是最好的,我宁愿通过模型来获取数据.但是我似乎无法使用模型从服务器获得相同的回报.

I had this kind of working before but i was getting a JSON response through a collection(it look okay), which i later found was not the best, and I would rather get data through models. But I can not seem to get the same return from the server using models.

这是我的代码: 型号:

Here is my code: Model:

        var CalendarDay = Backbone.Model.extend({ 
  urlRoot: daysURL      
});

var calendarItem = new CalendarDay();

我不应该能够执行此console.log(calendarItem.fetch())并获得正确的JSON响应吗?

Shouldn't I just be able to do this console.log(calendarItem.fetch()) and get the correct JSON response?

这是我的控制台显示的内容:

This is what my console shows:

我想要的只是"responseText"药水...

What i want is the "responseText" potion only...

这也意味着使用calendarItem.get(*****)不起作用.

This also means that using calendarItem.get(*****) doesnt work.

有什么想法的女士/男士吗?

Any ideas ladies/gents?

推荐答案

在屏幕快照中看到的是fetch返回的jqXHR对象.该对象表示异步ajax调用,而不是响应文本.

What you see in the screenshot is the jqXHR object returned by fetch. This object represents the asynchronous ajax call rather than the response text.

responseText是一个列表,而不是单个对象.因此,calendarItem将包含列表而不是item属性.在calendarItem.attributes上尝试console.log(在等待fetch完成之后.)

The responseText is a list rather than a single object. So the calendarItem will contain the list rather than the item attributes. Try a console.log on calendarItem.attributes (after waiting for fetch to finish.)

如果只想获取项目,则可以将函数用作模型的url属性-然后可以将ID传递给模型,然后.url()返回该日历项目的资源位置.

If you only want to fetch on item you can use a function as the url property of your model - then you can pass an ID to the model and .url() returns the resource location for that calendar item.

这篇关于骨干模型仅返回和对象,或JSON {readyState:1}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆