推json敲出可观察的数组
I have radio buttons which are observable and depending on the value i want to trigger a GETJSON method to return my data and then push it to an observable array that i am displaying as a list. The error i am getting is Cannot read property 'push' of undefined. Here is my Fiddle
我有可观察的单选按钮,根据我想要触发GETJSON方法的值来返回我的数据,然后将其推送到我正在显示为列表的可观察数组。我得到的错误是无法读取undefined的属性'push'。这是我的小提琴
How do i get this Data to my observable Array after the radio button is clicked triggering the getjson method?
在单击单选按钮触发getjson方法后,如何将此数据输入到我的可观察数组?
self.currentAnswer = ko.observable();
self.recipientList = ko.observableArray([]);
self.currentAnswer.subscribe(function (newValue) {
if (newValue == 'Internal') {
$.getJSON('GetInfo', function (data) {
var result = $.parseJSON(data);
self.recipientList.push(result);
});
2 个解决方案
#1
1
Things to consider:
需要考虑的事项:
$.getJSON()
will parse the JSON for you, you never need to do that yourself. This is also true for$.get()
. (*)- $ .getJSON()将为您解析JSON,您自己永远不需要这样做。 $ .get()也是如此。 (*)
- Knockout observables are functions. You can use them as callbacks. (**)
- Knockout observables是函数。您可以将它们用作回调。 (**)
With this knowledge:
有了这些知识:
self.recipientList = ko.observableArray();
self.currentAnswer = ko.observable();
self.currentAnswer.subscribe(function (newValue) {
if (newValue == 'Internal') {
$.get('GetInfo').done(self.recipientList);
}
});
(*) If you do not get parsed JSON in your success callback, fix the Content-Type
header of your response.
(*)如果在成功回调中没有解析JSON,请修复响应的Content-Type标头。
(**) If you call an observable and pass a value as first argument, it will store that value. Coincidentally jQuery calls Ajax success callbacks passing the returned value as the first parameter. Perfect fit.
(**)如果调用一个observable并将值作为第一个参数传递,它将存储该值。巧合的是,jQuery调用Ajax成功回调,将返回值作为第一个参数传递。完美契合。
更多相关文章
- 我可以禁用“后退”按钮浏览器功能吗?
- 如何禁用IE和Firefox中的后退按钮? [重复]
- 如何获取knockoutjs可观察数组的下一个元素?
- 使用Jquery Ajax更改按钮的颜色(从外部PHP文件接收颜色)
- 显示json数组中的所有项目
- 图表。js数据数组使用PHP, MySQL。如何从JSON数组定义数据源?
- 将JavaScript对象转换为要插入关系数据库的数组
- javascript数组和对象是否有设置顺序?
- 从另一个数组中删除数组的内容。