简体   繁体   中英

How to check json object is null or not?

I'm trying to validate form using jquery validation plugin.I'm trying whether the emailId is there in table,if not allowing to signup otherwise not. But,It's showing json object is greater than zero always.I tried using getjson, it doesnt works in localhost. So, where am i going wrong?

login.jsp

$('input#emailId').blur( function() {
                    $.ajax({
                        url:'/getUserByEmailOrPhone-'+$('#emailId').val()+'-'+$('#contactNumber').val(),
                        type:'GET',
                        data: 'json',
                        success:function(json){
                            if (json.length == 0)
                                {
                                    $('.email-check').append('<span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true" id="tt2" clientidmode="Static" runat="server" Visible="False"></span>');
                                  }
                            else{
                                  alert('user exists');
                                  $('.email-check').append('<span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true" id="tt2" clientidmode="Static" runat="server" Visible="False"></span>');
                            }
                        }
                });
            });

AppController.java

@RequestMapping(value={"/getUserByEmailOrPhone-{emailId}-{contactNumber}"}, method = RequestMethod.GET)
public @ResponseBody User getUserByEmailOrPhone(@PathVariable("contactNumber") String contactNumber,
        @PathVariable("emailId") String emailId,ModelMap model){
        return service.getUserByEmailOrPhone(emailId, contactNumber);
}

If the json variable contains correct values then you can do as-

success:function(json)
{
if (json === undefined) {
//your actions
}...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM