使用 CfHttp 在 Fetch POST 和 ColdFusion 函数之间进行通信
Communicate Between Fetch POST And ColdFusion Function With CfHttp
虽然我知道 fetch()
请求可以有参数,形式为 "headers:"、"body:" 等,但我在使用 fetch()
时遇到了问题POST 调用 ColdFusion 组件远程函数。
我的获取调用是:
fetch('myComponent.cfc?method=createObj', {method: "POST", body: jsonVar })
.then(function (getCreateResponse) {
//do great things
})
.catch(function err) {
alert("Fetch error: " + err);
});
我的 cfc 函数如下所示:
remote array function createObj(required any myObj) returnFormat = "JSON" {
cfhttp(url="http://myServer/ObjAPI/Obj", method="POST", result="getResponse") {
cfhttpparam(type="header", name="Content-Type", value="application/json");
cfhttpparam(type="body", value=SerializeJSON(myObj));
} }
(这个 POST 需要一个带有 JSON 的 RequestBody。)当我 运行 这个代码时,我的 CFC 日志告诉我:
"The MYOBJ parameter to the createObj function is required but was not passed in."
当我从 createObj 函数中删除参数时,提取调用失败,我被告知:
"Variable MYOBJ is undefined."
在我看来,CF 函数需要一个参数才能知道它应该在 cfhttp 调用中发送什么;但是,它无法识别 fetch 调用发送的 "body: jsonVar" 参数。是否有另一种方法来发送 CF 函数可以理解的参数?
您正在将数据作为 body
数据传递。您可以看到json数据如下
remote array function createObj() returnFormat = "JSON" {
WriteDump(deserializeJSON(ToString(getHTTPRequestData().content)));
myObj = deserializeJSON(ToString(getHTTPRequestData().content));
cfhttp(url="http://myServer/ObjAPI/Obj", method="POST", result="getResponse") {
cfhttpparam(type="header", name="Content-Type", value="application/json");
cfhttpparam(type="body", value=SerializeJSON(myObj));
}
}
虽然我知道 fetch()
请求可以有参数,形式为 "headers:"、"body:" 等,但我在使用 fetch()
时遇到了问题POST 调用 ColdFusion 组件远程函数。
我的获取调用是:
fetch('myComponent.cfc?method=createObj', {method: "POST", body: jsonVar })
.then(function (getCreateResponse) {
//do great things
})
.catch(function err) {
alert("Fetch error: " + err);
});
我的 cfc 函数如下所示:
remote array function createObj(required any myObj) returnFormat = "JSON" {
cfhttp(url="http://myServer/ObjAPI/Obj", method="POST", result="getResponse") {
cfhttpparam(type="header", name="Content-Type", value="application/json");
cfhttpparam(type="body", value=SerializeJSON(myObj));
} }
(这个 POST 需要一个带有 JSON 的 RequestBody。)当我 运行 这个代码时,我的 CFC 日志告诉我:
"The MYOBJ parameter to the createObj function is required but was not passed in."
当我从 createObj 函数中删除参数时,提取调用失败,我被告知:
"Variable MYOBJ is undefined."
在我看来,CF 函数需要一个参数才能知道它应该在 cfhttp 调用中发送什么;但是,它无法识别 fetch 调用发送的 "body: jsonVar" 参数。是否有另一种方法来发送 CF 函数可以理解的参数?
您正在将数据作为 body
数据传递。您可以看到json数据如下
remote array function createObj() returnFormat = "JSON" {
WriteDump(deserializeJSON(ToString(getHTTPRequestData().content)));
myObj = deserializeJSON(ToString(getHTTPRequestData().content));
cfhttp(url="http://myServer/ObjAPI/Obj", method="POST", result="getResponse") {
cfhttpparam(type="header", name="Content-Type", value="application/json");
cfhttpparam(type="body", value=SerializeJSON(myObj));
}
}