@RequestParam 没有收到值
@RequestParam is not receiving value
在我的应用程序中,我想在用户输入我的 phone 数字输入字段后检查该 phone 数字是否存在于数据库中,我使用 onchange
事件立即在输入字段上发送数字问题是当我从此输入字段发送数据以检查@RequestParam未收到数据并且我的值变为空时请帮助我在这里我做错了什么以及应该做什么完成。
这是我的代码:
控制器
@Controller
@RequestMapping("/indoUser")
public class UserController {
@ResponseBody
@RequestMapping(value="/validate-phn",method = RequestMethod.POST)
public APIResponseModal checkPhonePresent(@ModelAttribute String phoneNumber) {
logger.info("Check Phone MEthod");
logger.info(phoneNumber);
List<String> errorList = new ArrayList<String>();
APIResponseModal apiResponse = new Utils().getDefaultApiResponse();
try {
if(Utils.isNotNull(phoneNumber)) {
Boolean isExists = userService.phoneNumberExists(phoneNumber, errorList);
if(isExists) {
apiResponse.setMessage("Number already Present please login with OTP !");
apiResponse.setStatus(HttpStatus.BAD_REQUEST);
}else {
apiResponse.setMessage("Number is New ");
apiResponse.setStatus(HttpStatus.OK);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return apiResponse;
}
}
输入字段
function fetchAndDisplayCountryList(){
var ajaxObject = new MasterAjax();
ajaxObject.requestType = "POST";
ajaxObject.url = "getCountryList";
ajaxObject.contentType = false;
ajaxObject.data = null;
ajaxObject.requestData(function(responseData) {
if (responseData.status == "OK") {
countryList = JSON.parse(responseData.data);
let content = "<select style=\"padding: 0;\" id=\"leadCountry\" onchange=\"displayISDCode();\" class=\"inputTxt\" required=\"required\" >" +
"<option value=\"-1\"> Country </option>";
for(let k=0;k<countryList.length;k++){
content += "<option value="+countryList[k].id+">"+countryList[k].countryName+"</option>";
}
content += "</select>";
$(".country").html(content);
} else {
//NO COUNTRY LIST RECEIVED
}
});
}
function displayISDCode(){
let countryId = document.getElementById("leadCountry").value;
let isdCode = null;
for (var i = 0; i < countryList.length; i++) {
if(countryList[i].id == countryId){
isdCode = countryList[i].countryPhoneCode;
}
}
phoneContent= "<input type=\"tel\" class=\"inputTxt\" id=\"phoneNumber\" value="+isdCode+" maxlength=\"15\" onchange=\"checkPhoneNumber();\" name=\"phoneNumber\" required=\"required\" placeholder=\"\">"+
"<label for=\"\"class=\"inputlab\">Phone Number</label>";
$(".phonediv").html(phoneContent);
}
AJAX 请求
function checkPhoneNumber(){
console.log("Check phone is called")
// let formData = new FormData();
// formData.append("phoneNumber", $("#storeOpenTime").val());
let phone = document.getElementById("phoneNumber").value;
console.log("Value of phone is :" + phone);
var obj = new MasterAjax();
obj.requestType = "POST";
obj.url = "indoUser/validate-phn";
obj.data = phone;
console.log("Before sending the data Date is : : "+obj.data)
obj.contentType = false;
obj.processData = false;
obj.dataType= "application/json";
obj.requestData(function(responseData){
console.log(responseData);
if(responseData.status == "OK" || responseData.status == "ok"){
// toastSuccessAlert(responseData.message);
console.log(responseData)
}else{
// toastDangerAlert("Failed",responseData.message)
console.log(responseData)
}
});
}
浏览器控制台
data: null
message: "Unable to process the request!"
status: "BAD_REQUEST"
IDE 控制台
Check Phone MEthod
2021-05-23 12:24:06.142 INFO 14972 --- [nio-8080-exec-5] c.I.e.U.Controller.UserController : null
@RequestBody 为我工作而不是@ModelAttribute
在我的应用程序中,我想在用户输入我的 phone 数字输入字段后检查该 phone 数字是否存在于数据库中,我使用 onchange
事件立即在输入字段上发送数字问题是当我从此输入字段发送数据以检查@RequestParam未收到数据并且我的值变为空时请帮助我在这里我做错了什么以及应该做什么完成。
这是我的代码:
控制器
@Controller
@RequestMapping("/indoUser")
public class UserController {
@ResponseBody
@RequestMapping(value="/validate-phn",method = RequestMethod.POST)
public APIResponseModal checkPhonePresent(@ModelAttribute String phoneNumber) {
logger.info("Check Phone MEthod");
logger.info(phoneNumber);
List<String> errorList = new ArrayList<String>();
APIResponseModal apiResponse = new Utils().getDefaultApiResponse();
try {
if(Utils.isNotNull(phoneNumber)) {
Boolean isExists = userService.phoneNumberExists(phoneNumber, errorList);
if(isExists) {
apiResponse.setMessage("Number already Present please login with OTP !");
apiResponse.setStatus(HttpStatus.BAD_REQUEST);
}else {
apiResponse.setMessage("Number is New ");
apiResponse.setStatus(HttpStatus.OK);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return apiResponse;
}
}
输入字段
function fetchAndDisplayCountryList(){
var ajaxObject = new MasterAjax();
ajaxObject.requestType = "POST";
ajaxObject.url = "getCountryList";
ajaxObject.contentType = false;
ajaxObject.data = null;
ajaxObject.requestData(function(responseData) {
if (responseData.status == "OK") {
countryList = JSON.parse(responseData.data);
let content = "<select style=\"padding: 0;\" id=\"leadCountry\" onchange=\"displayISDCode();\" class=\"inputTxt\" required=\"required\" >" +
"<option value=\"-1\"> Country </option>";
for(let k=0;k<countryList.length;k++){
content += "<option value="+countryList[k].id+">"+countryList[k].countryName+"</option>";
}
content += "</select>";
$(".country").html(content);
} else {
//NO COUNTRY LIST RECEIVED
}
});
}
function displayISDCode(){
let countryId = document.getElementById("leadCountry").value;
let isdCode = null;
for (var i = 0; i < countryList.length; i++) {
if(countryList[i].id == countryId){
isdCode = countryList[i].countryPhoneCode;
}
}
phoneContent= "<input type=\"tel\" class=\"inputTxt\" id=\"phoneNumber\" value="+isdCode+" maxlength=\"15\" onchange=\"checkPhoneNumber();\" name=\"phoneNumber\" required=\"required\" placeholder=\"\">"+
"<label for=\"\"class=\"inputlab\">Phone Number</label>";
$(".phonediv").html(phoneContent);
}
AJAX 请求
function checkPhoneNumber(){
console.log("Check phone is called")
// let formData = new FormData();
// formData.append("phoneNumber", $("#storeOpenTime").val());
let phone = document.getElementById("phoneNumber").value;
console.log("Value of phone is :" + phone);
var obj = new MasterAjax();
obj.requestType = "POST";
obj.url = "indoUser/validate-phn";
obj.data = phone;
console.log("Before sending the data Date is : : "+obj.data)
obj.contentType = false;
obj.processData = false;
obj.dataType= "application/json";
obj.requestData(function(responseData){
console.log(responseData);
if(responseData.status == "OK" || responseData.status == "ok"){
// toastSuccessAlert(responseData.message);
console.log(responseData)
}else{
// toastDangerAlert("Failed",responseData.message)
console.log(responseData)
}
});
}
浏览器控制台
data: null
message: "Unable to process the request!"
status: "BAD_REQUEST"
IDE 控制台
Check Phone MEthod
2021-05-23 12:24:06.142 INFO 14972 --- [nio-8080-exec-5] c.I.e.U.Controller.UserController : null
@RequestBody 为我工作而不是@ModelAttribute