【问题标题】:Nodejs redirect return NULLNodejs重定向返回NULL
【发布时间】:2019-03-18 02:39:42
【问题描述】:

我正在构建一个 WordPress woocommerce 支付插件,以及一个在 Nodejs 后端服务器中用于连接的 API。

我正在使用 curl 正常调用 API ('https://test.com/test'):

$curl = curl_init($url);
    curl_setopt_array($curl, array(
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HEADER => false,
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_POSTFIELDS => json_encode($body)
    ));

    $result = curl_exec($curl);
    curl_close($curl);

    $response = json_decode($result);

然后在我的 API 中,我必须接收并检查 POST 参数,然后重定向到另一个路由器 ('https://test.com/render) 以呈现 ejs 视图页面。 我尝试使用

res.redirect(307, 'https://test.com/test')

res.writeHead(307, { 'Location': 'https://test.com/test' });
res.end();

或者直接渲染页面

res.render('index', { merchantUser: UserID });

甚至

window.location.href = 'https://test.com/test'

但是前三个直接返回NULL,最后一个返回'window not defined'。而且它们都没有到达重定向路由器('https://test.com/render)。

我在网上能找到的就这些了,谁能帮帮我或者给我一些建议?非常感谢。

【问题讨论】:

  • 你能显示你的代码来接收你想要重定向的api吗
  • @mooga 在我的问题中,我尝试了 res.redirect()、res.writeHead()、res.render()。在此之前,我只是收集数据并检查它们是否有效。

标签: php node.js wordpress


【解决方案1】:
  • 我建议你等待 NodeJs 的第一个 API 的响应并检查响应 http 代码,如果是 200 则再点击另一个 NodeJs API。

  • 或者像这样从 NodeJs API 发送响应并在 php 中回显响应:

    res.send('<script>window.location.href="your URL";</script>');

  • 或者一次完成所有 NodeJs 进程并发送一个 ejs 视图页面 (html) 作为对 php curl 请求的响应

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 2022-12-29
    • 2023-03-19
    • 2021-10-30
    • 2018-05-08
    • 2014-08-07
    • 1970-01-01
    相关资源
    最近更新 更多