用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;
		}
	});
}

猜你喜欢

转载自blog.csdn.net/qq_55342245/article/details/130930553