前端特殊字符数据,后端接收产生错乱,前后端都需要处理

前端:

const data = {
  createTime: "2024-06-11 09:58:59",
  id: "1800346960914579456",
  merchantId: "1793930010750218240",
  mode: "DEPOSIT",
  channelCode: "if(amount >= 50){'iugu2pay';} else if(amount < 10){'iugupay';} else {'todaypay';}",
  settleRate: 8,
  settleFee: 8,
  conditionSettleRate: "if(amount >= 5){ 0.2;} else{0.88;}",
  name: "HaliAntpay"
};

// Encode the fields
data.channelCode = encodeURIComponent(data.channelCode);
data.conditionSettleRate = encodeURIComponent(data.conditionSettleRate);

// Send the encoded data
fetch('http://your-backend-endpoint/yourEndpoint', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(data)
})
.then(response => response.json())
.then(result => console.log(result))
.catch(error => console.error('Error:', error));

后端:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

@RestController
public class YourController {

    @PostMapping("/yourEndpoint")
    public AjaxResult yourMethod(@RequestBody String rawRequestBody) throws IOException {
        System.out.println("Received raw JSON: " + rawRequestBody);

        ObjectMapper mapper = new ObjectMapper();
        MerchantChnlSetting merchantChnlSetting = mapper.readValue(rawRequestBody, MerchantChnlSetting.class);

        // Decode the fields
        String decodedChannelCode = URLDecoder.decode(merchantChnlSetting.getChannelCode(), StandardCharsets.UTF_8.name());
        String decodedConditionSettleRate = URLDecoder.decode(merchantChnlSetting.getConditionSettleRate(), StandardCharsets.UTF_8.name());

        // Set the decoded values back to the object
        merchantChnlSetting.setChannelCode(decodedChannelCode);
        merchantChnlSetting.setConditionSettleRate(decodedConditionSettleRate);

        System.out.println("Mapped Object: " + merchantChnlSetting);
        // Continue processing
        return AjaxResult.success();
    }
}





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有时间指导毕业设计

觉得写的好的话可以给我打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值