2017-06-19 41 views

回答

0

在您的反应组件中,调度被中间件捕获的操作。中间件应该调用jsonp端点,可能使用jquery的$ .ajax方法。我也喜欢fetchJsonp库。然后,您的中间件应该使用ajax或fetchJsonp收到的数据发送新的动作,这些数据应该由reducer捕获,并更改状态。

1

我在大部分使用fetch箱子反应应用:

// payload is your post data 
    const payload = {data: 'test'}; 
    const options = { 
     method: 'POST', 
     headers: { 
     'Accept': 'application/json', 
     'Content-Type': 'application/json', 
     }, 
     body: JSON.stringify(payload), 
     cors: true, // allow cross-origin HTTP request 
     credentials: 'same-origin' // This is similar to XHR’s withCredentials flag 
    }; 

    // SEND REQUEST 
    fetch('http://example.com/', options).then((response) => { 
     // TODO 
    }).catch((error) => { 
     // TODO 
    });