在回流中,如何从asp.net Web API获取数据 [英] in reflux, how can i get data from asp.net web api

查看:177
本文介绍了在回流中,如何从asp.net Web API获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用reactjs的回流.从Store的回流操作中,我编写了一个从asp.net Web api获取数据的操作,我认为这只是获取用于ajax的数据的唯一方法,有人跟我说,我可以使用插件jquery来获取数据,但是我不知道不要相信,因为$ .ajax是最好的方法.我在Google上搜索了所有内容,但没有找到解决方法.如果您知道要解决的问题,请与我分享,非常感谢.

i'am using reflux of reactjs. From Store's action of reflux, i wrote a action to get data from asp.net web api, i think that just only way to get data which use to ajax, someone say with me, i can get data with a plugin jquery but i don't believe it because the $.ajax is best way. I search everything on google but i don't see to resolve for that. If you know something to resolve, please share with me, i really thanks.

此外,我对ajax的全局和局部变量有疑问.请查看我的代码,您会看到从不返回任何值的粗体文本,成功块存在的问题,列表var在块外部时未更新.这是怎么回事?我该如何解决该错误?

Beside, i had a problem with global and local variable of ajax. Please review my code, you can see the bold text which never return a value, the problem which stay with the block of success, the list var isn't update when it outside of the block. What's up problem with that ? how can i fix that error ?

再次非常感谢您!

(function (Reflux, WorkHistoryActions, global) {

    global.workhistoryStore = Reflux.createStore({

        listenables: [WorkHistoryActions],

        init: function () {
            this.storyArr = [];
        },

        getItems: function (resume_id) {
            console.log(resume_id)
            **var list = [];**
            $.ajax({
                type: "get",
                url: global.getHost() + "/api/workhistories/6969607988340821009",
                dataType: 'json',
                crossDomain: true,
                success: function (data) {

                    $.each(data, function (i, v) {
                        **list.push(v);**
                    })

                }
            });
            **return list;**
        },
})

推荐答案

我不确定如何在回流中进行操作,但是在普通的助焊剂架构中,您会这样做:

I am not sure how to do it in reflux but in a normal flux architecture you would do like so:

    getItems: function (resume_id) {
        $.ajax({
            type: "get",
            url: global.getHost() + "/api/workhistories/6969607988340821009",
            dataType: 'json',
            crossDomain: true,
            success: function(result) {
                workHistoryActionCreator.receiveItems(result);
            },
            error: function(error) {
                workHistoryActionCreator.failToReceiveItems(error);
            }
        });
    }

商店向调度程序注册RECEIVED_WORK_HISTORY_ITEMS和FAILED_TO_RECEIVE_WORK_HISTORY_ITEMS操作,设置其数据,然后触发更改事件.

The store registers to the dispatcher for the actions RECEIVED_WORK_HISTORY_ITEMS and FAILED_TO_RECEIVE_WORK_HISTORY_ITEMS, sets its data and then fires the change event.

这篇关于在回流中,如何从asp.net Web API获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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