用form表单,向后端参数

相同部分

网页代码

<div style="margin-top: 45px;">
	<form>
		<div>
			<label>类型</label>
			<select name="smsType" id="smsType">
				<option value="0">--请选择--</option>
				<option value="auto">单次发送</option>
				<option value="order">重复发送</option>
			</select>
		</div>
		<div>
			<label>发送区域</label>
			<select name="areaName" id="areaName">
				<option value="0">--请选择--</option>
				<option value="区域三">区域一</option>
				<option value="区域四">区域二</option>
			</select>
		</div>
		<div>
			<label>发送</label>
			<select name="target" id="target">
				<option value="0">--请选择--</option>
				<option value="ac2">固定范围</option>
				<option value="ac3">指定范围</option>
			</select>
		</div>
		<div>
			<label>内容</label>
			<textarea name="content" id="content"></textarea>
		</div>
	</form>
	<div>
		<button type="submit" onclick="sub()">表单提交</button>
		<button type="reset">Reset重置</button>
	</div>
</div>

传给后端java实体对象

java接收方,

@RequestMapping(value = "online/SendSms",method = RequestMethod.POST)
public String SendSms(@RequestBody SmsMission smsMission){
	String smsType = smsMission.getSmsType();
    String areaName = smsMission.getAreaName();
    String target = smsMission.getTarget();
    String content = smsMission.getContent();
	return "OK"
} 

JS发送方,

function sub(){
	var form={};
	form.smsType = $("#smsType").val();
	form.areaName = $("#areaName").val();
	form.target = $("#target").val();
	form.content = $("#content").val();
	
	$.ajax({
		type: "POST", 
		url: "http://127.0.0.1:8029/population/online/SendSms",
		data: JSON.stringify(form),
		contentType : "application/json;charset=UTF-8",
		dataType: "json",
		success: function (res) {
			console.log(res);
		},
		error: function (errorMsg) {
			return errorMsg;
		}
	});
}

直接传给后端字符串

java接收方,

@RequestMapping(value = "online/SendSms",method = RequestMethod.POST)
public MessageVo SendSms(
            @RequestParam(name = "smsType",required = true) String smsType,
            @RequestParam(name = "areaName",required = true) String areaName,
            @RequestParam(name = "target",required = true) String target,
            @RequestParam(name = "content",required = true) String content
    ){

	Map<String,Object> respData = new HashMap<>();
    respData.put("类型",smsType);
    respData.put("发送区域",areaName);
    respData.put("发送",target);
    respData.put("内容",content);
    
	return "OK"
} 

JS发送方,

function sub(){
	var form={};
	form.smsType = $("#smsType").val();
	form.areaName = $("#areaName").val();
	form.target = $("#target").val();
	form.content = $("#content").val();
	
	$.ajax({
		type: "POST", 
		url: "http://127.0.0.1:8029/population/online/SendSms",
		data: form,
		contentType : "application/x-www-form-urlencoded",
		dataType: "json",
		success: function (res) {
			console.log(res);
		},
		error: function (errorMsg) {
			return errorMsg;
		}
	});
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值