我们如何在表单发布中传递HTTP请求正文中的JSON数据

编程入门 行业动态 更新时间:2024-10-24 06:34:34
本文介绍了我们如何在表单发布中传递HTTP请求正文中的JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们有一个表单,正在将数据提交到新标签页中.喜欢,

We had a form which was submitting the data to a page in a new tab. Like,

<form name='formOne' action='/action.cfm' method='post' target='_blank'> <input type='hidden' name='employee' value='{"first_name": "test","last_name":"name"}' /> <input type='hidden' name='contact' value='{"phone": "1233214090","fax":"1098760982"}' /> <input type="submit" /> </form>

但是现在"action.cfm"页面在http请求正文中需要一个JSON值.喜欢

But now "action.cfm" page is expecting a JSON value in http request body. Like

{ "employee": { "first_name": "test", "last_name": "name" }, "contact": { "phone": "1233214090", "fax": "1098760982" } }

在这种情况下,不确定如何在表单请求中的http请求正文中发送JSON数据.请建议是否可以这样做,或者是否有其他方法可以实现此目的.

Not sure how could we send the JSON data in http request body in form post in this case. Please suggest if it is possible to do so or if there is any other approach to achieve this.

推荐答案

在ColdFusion中,这是在发布请求的正文中发送json的方式:

In ColdFusion, this is how you send json in the body of a post request:

string function postAsJson( required struct data) { var responseStr = ""; try { var http = new http(argumentCollection={ "method": "post", "timeout": 50, "encodeUrl": false }); http.addParam(type="body", value=serializeJSON(Arguments.data)); http.addParam(type="header", name="content-type", value="application/json"); http.setURL("your form handler"); var httpResult = http.send().getPrefix(); if (httpResult.status_code == 200) { responseStr = httpResult.fileContent; } } catch (any err) { responseStr = "<p>#err.message#</p>"; } return responseStr; } myData = { "this": "and", "that": true }; result = postAsJson(myData); writeOutput(result);

在请求处理程序中,您将获得如下数据:

And in your request handler, you get the data like this:

requestData = getHttpRequestData(); if (isJSON(requestData.content)) { myData = deserializeJSON(requestData.content); writeDump(myData); } else { writeOutput("<p>Invalid request</p>"); }

(我尚未在ACF中对此进行过测试,但我知道它确实在Lucee-5.2.x中有效)

更多推荐

我们如何在表单发布中传递HTTP请求正文中的JSON数据

本文发布于:2023-10-29 09:52:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539386.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   文中   数据   如何在   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!