首页 > 其他分享 >使用js语文json字符串通过get方式传值,然后用request接收

使用js语文json字符串通过get方式传值,然后用request接收

时间:2024-04-23 13:11:22浏览次数:18  
标签:jsonString const get request js JSON 字符串 data

在JavaScript中,如果你有一个JSON字符串,并且你想通过GET请求或者其他HTTP请求来传递这个字符串,你可以使用encodeURIComponent函数来确保字符串能够安全地通过URL传输。

// 假设我们有一个JSON对象
const jsonObject = {
  name: "John",
  age: 30,
  city: "New York"
};
 
// 将JSON对象转换为字符串
var jsonString = JSON.stringify(jsonObject);
 
// 编码JSON字符串以确保它可以通过URL传输
var encodedString = encodeURIComponent(jsonString);
 
// 创建GET请求的URL,附加参数data,其值为编码后的JSON字符串
var url = `/api/data?data=${encodedString}`;
 

在服务器端,你需要解析传递过来的参数,并且对其进行必要的解码操作:

// 假设你使用Node.js和Express作为服务器
const express = require('express');
const app = express();
 
app.get('/api/data', (req, res) => {
  const jsonString = req.query.data;
  const decodedString = decodeURIComponent(jsonString);
  const parsedData = JSON.parse(decodedString);
  res.json(parsedData);
});
 
app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

这里关键点,先通过decodeURIComponent解码再使用

标签:jsonString,const,get,request,js,JSON,字符串,data
From: https://www.cnblogs.com/firstcsharp/p/18152651

相关文章

  • js逆向实战之一品威客signature参数解密
    url:https://www.epwk.com/login.html分析过程输入用户名和密码,看触发的流量包。signature参数明显是被加密过的,接下来就是去寻找加密的过程。关键词搜索signature。有两处,第二处是个固定值不需要看,关注点在第一处。点进去看对应的代码,并打断点,重新登录,触发该断点。......
  • js递归实现深拷贝
    functiongetType(data){returnObject.prototype.toString.call(data).replace(']','').substring(8)}function_clone(data,res){consttype=getType(data)if(type==='Object'){Object.keys(data).map(i=>{......
  • js AddDays ,AddYears
    //日期类型变量格式化,默认格式"xxxx-xx-xx"Date.prototype.Format=function(fmt){fmt=fmt||"yyyy-MM-dd";varo={"M+":this.getMonth()+1,//月份"d+":this.getDate(),//日"h+":......
  • three.js使用Instanced Draw+Frustum Cull+LOD来渲染大场景(开源)
    大家好,本文使用three.js实现了渲染大场景,在移动端也有较好的性能,并给出了代码,分析了关键点,感谢大家~关键词:three.js、InstancedDraw、大场景、LOD、FrustumCull、优化、Web3D、WebGL、开源代码:Github我正在承接Web3D数字孪生项目,具体介绍可看承接各种Web3D业务加QQ群交流:106......
  • fastjson导致的程序崩溃:A fatal error has been detected by the Java Runtime Enviro
    ##AfatalerrorhasbeendetectedbytheJavaRuntimeEnvironment:##EXCEPTION_ACCESS_VIOLATION(0xc0000005)atpc=0x000001da4d3ab6b3,pid=15996,tid=0x0000000000006478##JREversion:Java(TM)SERuntimeEnvironment(8.0_361)(build1.8.0_361-b09)......
  • gojs2.1去水印
    go.jsgo-debug.jsgo-module.jsES6模块go-debug-module.js这四个文件,具体看你使用哪一个搜索String.fromCharCode找到下图位置,这是我的格式化之后的样子 然后在 returnf 之前添加代码if(f.indexOf('GoJS2.1evaluation')>-1||f.indexOf('(c)1998-......
  • vis.js摄像机位置
    代码案例<!DOCTYPEhtml><html><head><title>Graph3Ddemo</title><style>body{font:10ptarial;}td{font:10ptarial;}</style><s......
  • vis.js绘制3d图形
    案例1代码案例<!DOCTYPEhtml><html><head><title>Graph3Ddemo</title><scripttype="text/javascript"src="https://unpkg.com/vis-graph3d@latest/dist/vis-graph3d.min.js"><......
  • vis.js分组多图折线图
    代码案例<!doctypehtml><html><head><title>Timeline</title><scripttype="text/javascript"src="https://unpkg.com/vis-timeline@latest/standalone/umd/vis-timeline-graph2d.min.js"></script>......
  • vis.js着色
    代码案例<!doctypehtml><html><head><title>Timeline</title><scripttype="text/javascript"src="https://unpkg.com/vis-timeline@latest/standalone/umd/vis-timeline-graph2d.min.js"></script>......