英文:
Call 'document / redirect' API inside express controller
问题
我有以下API调用的document / redirect
,如下所示:
它在HTML代码中被调用,如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta http-equiv="Content-Language" content="en-us" />
</head>
<body>
<form
name="autoForm"
action="http://test/Integration.htm"
method="post"
>
<input type="submit" name="submit" value="Continue" />
<input type="input" name="TEST_VALUE" value="test" />
</form>
</body>
</html>
现在我想在Express控制器中实现相同的逻辑。我需要实现一个控制器,调用这个外部API http://test/Integration.htm
。流程将如下:
前端 -> Express控制器 -> 调用 http://test/Integration.htm
-> 重定向前端页面
我尝试在Express中构建以下控制器,但没有成功:
export const ssoHandler: any = async (req: Request, res: Response) => {
const formData = new URLSearchParams();
const clientCandidateId = req.query.clientCandidateId as string;
formData.append('TEST_VALUE', clientCandidateId);
const response = await axios({
url: `${config.host}`,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
method: 'POST',
data: formData,
});
res.write(response.data);
};
但没有成功。我尝试在控制器中构建自定义HTML并发送回客户端。但我收到了CORS
错误。而且我不认为这是正确的方法。
是否有办法在Express中使用相同的方法?由于安全原因,我不能在前端代码中实现此要求。我必须在控制器中执行这个。
英文:
I have document / redirect
API call as shown below
It's being called inside HTML Code like this
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta http-equiv="Content-Language" content="en-us" />
</head>
<body>
<form
name="autoForm"
action="http://test/Integration.htm"
method="post"
>
<input type="submit" name="submit" value="Continue" />
<input type="input" name="TEST_VALUE" value="test" />
</form>
</body>
</html>
Now i want to implement the same logic within express controller. I need to implement a contorller which calls this external API http://test/Integration.htm
. The flow will be like this:-
Front-end -> Express controller -> Call the http://test/Integration.htm
-> Redirect the front-end page
I tried to build the below controller inside express
export const ssoHandler: any = async (req: Request, res: Response) => {
const formData = new URLSearchParams();
const clientCandidateId = req.query.clientCandidateId as string;
formData.append('TEST_VALUE', clientCandidateId);
const response = await axios({
url: `${config.host}`,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
method: 'POST',
data: formData,
});
res.write(response.data)
};
But no luck. I tried bulding a custom HTML insisde the controller and sending back to client. But i received CORS
error. Also i don't believe this is the right approach.
Is there any way to make the same approach using express ?. For security reasons. I can't implement this requirement inside the front-end code. I have to do it inside controller like this.
答案1
得分: 1
CORS错误基本上是跨平台阻止/预防。您可以通过运行命令 "npm i cors" 通过CLI 安装 cors,然后在您的 server.js 或 index.js 文件中写入:
const app = express();
app.use(cors);
希望这可以帮助解决问题。谢谢和问候。
英文:
Basically CORS error refers to cross platform blockage/prevention. You may install cors through cli by running command "npm i cors". and then in your server.js or index.js file write:
const app=express();
app.use(cors);
I hope this will help to solve. Thanks & regards.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论