如何显示来自api的ajax响应的错误消息?

时间:2018-09-15 11:49:33

标签: javascript jquery ajax api response

我已经使用jquery ajax发送了api请求,而我正在收到这样的ajax响应

Array([consumer_key] => ada691a715307861907d65d36d [consumer_secret] => a75e40ec1530786190b62316d1 [consumer_nonce] => 1537011049789 [consumer_device_id] => ldx0EWMCl3hNhJWCRIdPVveLy [] ],“ message”:“电子邮件地址已经存在”}

如果响应中包含“电子邮件地址已存在”,我想显示错误消息,否则警报消息“已成功提交”

$.validator.setDefaults({
		submitHandler: function() {
			alert("submitted!");
			
			
		 
		  var customers_firstname=$("#customers_firstname").val();
        var customers_lastname=$("#customers_lastname").val();
         var customers_telephone=$("#customers_telephone").val();
          var email=$("#email").val();
		  
		  
		var d = new Date();
    var consumer_nonce = d.getTime();
		
		U=url+'api/save_reg';
		//alert(consumer_nonce);
		//exit;
		//var ul="{{ url('/backend/api/save_reg') }}";
	
		  $.ajax({
        type:"POST",
        url:url+'api/save_reg',
       data:$("#postcontent").serialize(),
	/*	data:{
			    customers_firstname:customers_firstname,
			    customers_lastname:customers_lastname,
				 customers_telephone:customers_telephone,
			   email:email,
			   password:password
			   },*/
			    headers: {
        
        "consumer-key": consumer_key, 
		"consumer-secret": consumer_secret, 
		"consumer-nonce": consumer_nonce, 
		"consumer-device-id": consumer_device_id,  
    },
        beforeSend:function(){
            $(".post_submitting").show().html("<center><img src='https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/0.16.1/images/loader-large.gif'/></center>");
        },success:function(response){ 
		
		

	   alert(response);
    
 
		
		//-- alert(response[1]); 
            //alert(response);
			//response = response.toJSON();
			//alert(response);
            $("#return_update_msg").html(response); 
            $(".post_submitting").fadeOut(1000);                
        }
    });
	  
    
			
			
		}
	});
@extends('frontend.layouts.app')
    
    @section('title','Home')
     
    @section('body')
    
    

    
   
<!-- Main Container  -->
<div class="main-container container">
		<ul class="breadcrumb">
			<li><a href="#"><i class="fa fa-home"></i></a></li>
			<li><a href="#">Account</a></li>
			<li><a href="#">Register</a></li>
		</ul>
		
		<div class="row">
			<div id="content" class="col-sm-12">
				<h2 class="title">Register Account</h2>
				<p>If you already have an account with us, please login at the <a href="#">login page</a>.</p>
				<div id="return_update_msg" class="return_update_msg">j</div>
                <div class="post_submitting"></div>
                
                <form name="postcontent" class="cmxform" id="postcontent" >

					<fieldset id="account">
						<legend>Your Personal Details</legend>
						
						<div class="form-group required">
                    
							<label class="col-sm-2 control-label" for="customers_firstname">First Name</label>
							<div class="col-sm-10">
								<input type="text" name="customers_firstname" minlength="2"  placeholder="First Name" id="customers_firstname" class="form-control">
                                <span id="customers_firstname_error"></span>
							</div>
						</div>
                        
                        <div class="form-group required">
							<label class="col-sm-2 control-label" for="customers_lastname">Last Name</label>
							<div class="col-sm-10">
								<input type="text" name="customers_lastname" minlength="2"  placeholder="Last Name" id="customers_lastname" class="form-control">
                                 <span id="customers_lastname_error"></span>
							</div>
						</div>
						
						<div class="form-group required">
							<label class="col-sm-2 control-label" for="email">E-Mail</label>
							<div class="col-sm-10">
								<input type="email" name="email"  placeholder="E-Mail" id="email" class="form-control">
                                 <span id="email_error"></span>
							</div>
						</div>
						<div class="form-group required">
							<label class="col-sm-2 control-label" for="input-telephone">Mobile Number</label>
							<div class="col-sm-10">
								<input type="tel" name="customers_telephone"  placeholder="Telephone" id="customers_telephone" class="form-control">
                                  <span id="customers_telephone_error"></span>
							</div>
						</div>
						
					</fieldset>
					
					<fieldset>
						<legend>Your Password</legend>
						<div class="form-group required">
							<label class="col-sm-2 control-label" for="password">Password</label>
							<div class="col-sm-10">
								<input type="password" name="password"  placeholder="Password" id="password" class="form-control">
                                  <span id="password_error"></span>
							</div>
						</div>
						<div class="form-group required">
							<label class="col-sm-2 control-label" for="confirm_password">Password Confirm</label>
							<div class="col-sm-10">
								<input type="password" name="confirm_password"  placeholder="Password Confirm" id="confirm_password" class="form-control">
                                 <span id="password_confirm_error"></span>
							</div>
						</div>
					</fieldset>
					 <span id="success_message"></span>
					<div class="buttons">
							<input class="submit btn btn-primary" id="save_buttonk" type="submit" value="Submit">
							<!--<input type="submit" id="save_button" value="Continue" class="btn btn-primary">-->
						</div>
                        </form>
					</div>
			
			</div>
		</div>
        <br />
<!-- //Main Container -->

 @endsection
   @section('pagescript')
   
   

   <script type="text/javascript" src="{{ asset('public/frontend/qshopee')}}/js/registration.js"></script>
   
   <script>

$().ready(function() {
		// validate the comment form when it is submitted
		$("#commentForm").validate();
   	// validate signup form on keyup and submit
		$("#postcontent").validate({
			rules: {
				//customers_firstname: "required",
				//customers_lastname: "required",
				/*username: {
					required: true,
					minlength: 2
				},*/
				customers_firstname: {
					required: true,
					minlength: 2
				},
				customers_lastname: {
					required: true,
					minlength: 2
				},
				password: {
					required: true,
					minlength: 5
				},
				confirm_password: {
					required: true,
					minlength: 5,
					equalTo: "#password"
				},
				email: {
					required: true,
					email: true
				},
				/*topic: {
					required: "#newsletter:checked",
					minlength: 2
				},
				agree: "required"*/
			},
			messages: {
				//customers_firstname: "Please enter your firstname",
				//customers_lastname: "Please enter your lastname",
				/*username: {
					required: "Please enter a username",
					minlength: "Your username must consist of at least 2 characters"
				},*/
				customers_firstname: {
					required: "Please enter a firstname",
					minlength: "Your firstname must consist of at least 2 characters"
				},
				customers_lastname: {
					required: "Please enter a lastname",
					minlength: "Your lastname must consist of at least 2 characters"
				},
				password: {
					required: "Please provide a password",
					minlength: "Your password must be at least 5 characters long"
				},
				confirm_password: {
					required: "Please provide a password",
					minlength: "Your password must be at least 5 characters long",
					equalTo: "Please enter the same password as above"
				},
				email: "Please enter a valid email address"
				/*agree: "Please accept our policy",
				topic: "Please select at least 2 topics"*/
			}
		});

	
	
	});
 
   </script>
    
   @stop

1 个答案:

答案 0 :(得分:1)

尝试

if (response.indexOf('Email address is already exist') > -1)
{
alert("Email Already Registered");
 }
 else
 {
 alert("Registration Successful");