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成功回调,将返回值作为第一个参数传递。完美契合。

更多相关文章

  1. 我可以禁用“后退”按钮浏览器功能吗?
  2. 如何禁用IE和Firefox中的后退按钮? [重复]
  3. 如何获取knockoutjs可观察数组的下一个元素?
  4. 使用Jquery Ajax更改按钮的颜色(从外部PHP文件接收颜色)
  5. 显示json数组中的所有项目
  6. 图表。js数据数组使用PHP, MySQL。如何从JSON数组定义数据源?
  7. 将JavaScript对象转换为要插入关系数据库的数组
  8. javascript数组和对象是否有设置顺序?
  9. 从另一个数组中删除数组的内容。

随机推荐

  1. 如果我是Android面试官九
  2. Scrapy框架的使用之Scrapy通用爬虫
  3. 数据科学大佬的简历上都有哪些技能?
  4. 分布式爬虫的部署之Scrapyd-Client的使用
  5. 如何用Python递归地思考问题?
  6. Scrapy框架的使用之Scrapy框架介绍
  7. 还在抱怨pandas运行速度慢?这几个方法会颠
  8. 5个原因告诉你:为什么在成为数据科学家之
  9. 高效方法 | Jupyter Notebook 比你想象中
  10. 金州勇士4年3冠的成功秘诀!数据可视化分析