首页 > 解决方案 > 收到 ajax 响应后使用 const 变量

问题描述

我一直在研究 ReactJS。我在方法“fetch_data”中进行了 ajax 调用。一旦收到 ajax 响应,它就会调用另一个方法“setLookupFValues”。

现在,我想将变量“page”传递给“setLookupFValues”函数。我该怎么做?

export function fetchData(type, custom_name,keyword,page) {
    const controller = abortPreviousRequests('abortPreviousRequests')

    abortableFetch(`${ROOT_URL}/fetch_data.json?type=${type}&custom_name=${custom_name}&key_word=${keyword}&page=${page}`, {
            signal: controller.signal
        })
        .then((response) => {
            return response.json()
        }).then((json) => {
            this.state.setLookupFValues(json)
        }).catch((ex) => {
            if (ex.name === 'AbortError') {
                console.log('request aborted')
            }
            console.log('parsing failed', ex)
        })
}

标签: javascriptreactjsajax

解决方案


推荐阅读