在反应 - 终极版应用程序中使用JSONP

问题描述 投票:2回答:2

我只是想在请求引起CORS headersmissing headers问题的出路,因此倾向于使用JSONP

任何建议/想法如何在JSONP应用程序中使用react-redux?怎么样配置和使用呢?

reactjs http-headers cors jsonp
2个回答
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
    });

0
投票

在你的反应组件,派遣由中间件抓的动作。中间件应该给JSONP端点的呼叫,可能使用jQuery的$就法。我也很喜欢fetchJsonp库。然后,您应该中间件与派遣由AJAX或fetchJsonp接收到的数据,这应该由你的减速被抓住了新的动作,并改变状态。

© www.soinside.com 2019 - 2024. All rights reserved.