用CfHttp实现Fetch-POST与ColdFusion函数的通信

用CfHttp实现Fetch-POST与ColdFusion函数的通信,coldfusion,fetch-api,Coldfusion,Fetch Api,虽然我知道fetch()请求可以有参数,如“headers:”、“body:”等,但我在调用ColdFusion组件远程函数时遇到了问题。 我的取回电话是: fetch('myComponent.cfc?method=createObj', {method: "POST", body: jsonVar }) .then(function (getCreateResponse) { //do great things }) .catch(function err) { ale

虽然我知道
fetch()
请求可以有参数,如“headers:”、“body:”等,但我在调用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));
        } }
(本文使用带有JSON的RequestBody。)当我运行此代码时,CFC日志告诉我:

“createObj函数的MYOBJ参数是必需的,但未传入。”

当我从createObj函数中删除参数时,fetch调用失败,我被告知:

“变量MYOBJ未定义。”


在我看来,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));
     } 
}

如果你用括号括起来,
{method:“POST”,body:jsonVar}
,去掉了逗号,@DanBracuk我恐怕不太明白这个建议;用括号将已有的括号括起来?逗号也必须在那里,否则我会出错。我不会用fetch来处理这件事。IE不支持Fetch,因此您的站点将无法跨浏览器访问。