2017-07-15 15 views
8

मैंने कोडनिर्देशक ढांचे में ईमेल उपलब्धता की जांच के लिए एक सरल कोड AJAX अनुरोध बनाया है। लेकिन, मुझे हर बार त्रुटि मिलती है। और यह नहीं पता कि इसे कैसे हल किया जाए। नीचे मेरा पाद लेख जेएस स्क्रिप्ट है (jquery और अन्य बाहरी स्क्रिप्ट के बाद)।कोडनिर्देशक AJAX त्रुटि उपयोगकर्ता ईमेल जांच

<script> 

    $(document).ready(function() { 
     $("#loading").hide(); 
     $("#email").blur(function() { 
      var email_val = $("#email").val(); 
      var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-][email protected][a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/; 
      if (filter.test(email_val)) { 
       // show loader 
       $('#loading').show(); 
       jQuery.ajax({ 
        type: "POST", 
        url: "<?php echo site_url()?>users/check_email_ajax", 
        dataType: 'json', 
        data: { 
         '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>', 
         'email': 'email_val' 
      }, 

       success: function (response) { 
        if(response){ 
         $('#loading').hide(); 
         console.log(response.message); 
         $('#msg').html(response.message) 
         $('#msg').show().delay(10000).fadeOut(); 
        } 
       }, 
        error: function(error){ 
         $('#loading').hide(); 
         console.log("There is some errors on server : " + error.error); 
        } 
       }) 
      } 
     }); 
    }); 

और इस डाटाबेस

public function check_email_ajax(){ 
    // allow only Ajax request 
    if($this->input->is_ajax_request()) { 
     // grab the email value from the post variable. 
     $email = $this->input->post('email'); 
     // check in database - table name : users , Field name in the table : email 
     if(!$this->form_validation->is_unique($email, 'users.email')) { 
      // set the json object as output 
      $this->output->set_content_type('application/json')->set_output(json_encode(array('message' => 'The email is already taken, choose another one'))); 
     }else{ 
      $this->output->set_content_type('application/json')->set_output(json_encode(array('message' => 'you can use this email'))); 
     } 
    } 
} 

में और पंजीकरण फार्म मैं ईमेल इनपुट के लिए इस क्षेत्र है में ईमेल की जांच करने के लिए अपने उपयोगकर्ता नियंत्रक समारोह है:

<div class="col-sm-5"> 
       <input type="email" id="email" name="email" class="form-control"> 
       <span id="loading"><img src="<?php echo base_url(); ?>ajax-loader.gif" alt="Ajax Indicator" /></span> 
      <div id="msg"></div> 
      </div> 

लेकिन अब हर बार जब मैं इनपुट का मूल्य बदलता हूं। मुझे console.log पर त्रुटि मिलती है

There is some errors on server : function(){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this} 

कोई भी इसे ठीक करने के बारे में जानता है?

error: function (error) { 
    $('#loading').hide(); 
    console.log("There is some errors on server : " + error.error); 
} 

jQuery documentation से::

error
Type: Function(jqXHR jqXHR, String textStatus, String errorThrown) A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and cross-domain JSONP requests. This is an Ajax Event.

क्या आप console.log साथ प्रवेश कर रहे हैं jqXHR पैरामीटर है

+0

कृपया अपना वेब कंसोल खोलें और अपने अनुरोध हेडर/प्रतिक्रिया का स्क्रीन शॉट जोड़ें और JSON प्राप्त करें जो आपके AJAX फ़ंक्शन –

+0

द्वारा पोस्ट किया जाता है, इसके अलावा, क्या आप कंसोल.लॉग (त्रुटि) का आउटपुट जोड़ सकते हैं; –

उत्तर

3

क्या आप और एक सफल डिबग के बीच खड़ा है यह हिस्सा है। यह काम करना चाहिए:

error: function (jqXHR, errorType, error) { 
    $('#loading').hide(); 
    console.log(errorType + ": " + error); 
} 

एक बार जब आपको वास्तविक त्रुटि संदेश मिलता है तो आप समस्या की जड़ तक पहुंच सकते हैं।

+0

वह तेज़ था। बहुत बढ़िया! –

+0

धन्यवाद! 'फ़ंक्शन() ....' आउटपुट ने मुझे तुरंत सही दिशा में भेजा। – MySidesTheyAreGone

+0

धन्यवाद आपके उत्तर के लिए श्री @ एलेक्सेंडर हिगिन्स: मैंने आपकी टिप्पणी की तरह फ़ंक्शन बदल दिया है और मुझे यह त्रुटि मिलती है। पार्सररर: सिंटेक्स त्रुटि: अनपेक्षित टोकन

2

मैंने पहले इस अभ्यास को किया है, इसलिए यहां मेरी अजाक्स लिपि है जिसका मैंने उपयोग किया था। ईमेल उपलब्धता प्राप्त करने के लिए मैंने आपके द्वारा एक अलग दृष्टिकोण लिया है। मैंने नियंत्रक कोड नहीं रखा है क्योंकि यह आपके नियंत्रक के लिए पतला लगता है।

public function check_email_availability(){ 

    $data = $this->input->post(); 

    $this->form_validation->set_error_delimiters('<span class="error">', '</div>');  

    if (($this->form_validation->run('email_verification') == FALSE)){   
     echo form_error('email'); 
    } else {    
     unset($_POST); 
     echo '<span style="color:green;" id="emailIsValid">Available</span>'; 
    } 

} 

मैं config अंदर सत्यापन जोड़ लिया है:

$(document).ready(function() { 
     //Your Email on key press 
     $("#email").keyup(function() { 

     var email = $(this).val(); 
     if (email.length > 3) 
     {  
     $.ajax({ 
      type: 'POST', 
      url: emailAvailability_url, 
      data: { 
       email: email 
      }, success: function(data) { 

       // Your Ajax Resonse Here 

      } error: function (jqXHR, errorType, error) { 
       $('#loading').hide(); 
       console.log(errorType + ": " + error); 
      } 
     }); 

     } 
    }); 
}); 

इस नियंत्रक कोड है जो मैं प्रयोग किया जाता है।

'email_verification' => array(
      array(
        'field' => 'email', 
        'label' => 'Email', 
        'rules' => 'required|max_length[50]|valid_email|is_unique[table_name.email]', 
        'errors' => array(
          'required' => 'Email is required.', 
          'valid_email' => 'Plese Enter Valid Email', 
          'is_unique' => 'That Email is already taken. Try Again With New Email.' 
        ) 
      ) 
) 

उत्तर का इरादा बस मदद करने का प्रयास है। तो यदि आप अपनी विधि से चिपकना चाहते हैं तो यह पूरी तरह से आपकी पसंद है। धन्यवाद

+0

पर मैं सही ढंग से दर्ज करने के लिए ईमेल जांचना चाहता हूं EX: [email protected] लेकिन मुझे लगता है कि आपने केवल इनपुट की लंबाई में जांच की है। वैसे भी आपके उत्तर के लिए धन्यवाद –

+0

कोई महोदय, आपको यह गलत नहीं लगता है, मैंने 'email.length> 3' लिया है क्योंकि मैं यह जांचना चाहता हूं कि जब उपयोगकर्ता कुछ जोड़ता है (मैंने 'कीपअप' का उपयोग किया है) तो मैं इसे नियंत्रक और नियंत्रक को भेजता हूं पक्ष मैंने ईमेल की जांच करने के लिए सीआई सत्यापन का उपयोग किया है अद्वितीय है या नहीं। और AJAX प्रतिक्रिया में मुझे उससे संबंधित संदेश मिलेंगे। –

1

मुझे खेद है, मैंने समाधान की स्थापना की है। ऐसा इसलिए है क्योंकि मैंने उपयोगकर्ता नियंत्रक के निर्माता में प्रमाणीकरण का उपयोग किया है। मैंने फॉर्म एक्शन को दूसरे कंट्रोलर में बदल दिया है और इसे पूरा किया है। अब मुझे ईमेल उपलब्धता संदेश मिलता है लेकिन अगर इससे पहले कि मुझे कुछ भी नहीं मिला तो उसे लिया गया। कोई AJAX लोडिंग gif दिखाया गया है!

संबंधित मुद्दे