2013-07-21 26 views
0

我告訴你什麼,讓AJAX工作是在wazoo一個痛苦!我花了很長時間纔得到一個簡單的字符串通過,然後我有一個JSON數組工作,感覺很好,現在我試圖做一些調整,並再次打破了整個事情。爲什麼在給出ajax錯誤之後,我如何才能弄清楚發生了什麼?AJAX沒有張貼或接收明顯

的jQuery:

 $('#upload_form option[value="addnew"]').click(function(){ 

       // Show modal window 
       $('#add-new').modal('show'); 

       // Get the class 
       var Classofentry = $(this).attr("class"); 
       //console.log(Classofentry); 


       $('#add-new-submit').on('click', function(){     


        // Get new option from text field 
        var value = $('#add-new-text').val(); 
        console.log(value); 

        $.ajax({ 
         type: "POST", 
         url: "<?php echo site_url(); ?>main/change_options", 
         data: {new_option: value, new_option_class: Classofentry}, 
         //dataType: "html", 
         dataType: "json", 
         error: errorHandler, 
         success: success 
         }); 

        function success(data) 
        { 

        if (data[1]) // Only add new entry if unique 
        { 
         // Add new entry 
         //$('#animal_species').append("<option value='" + data + "'selected=\"selected\">" + data + "</option>"); 
         $('#'+Classofentry).append("<option value='" + data[0] + "'selected=\"selected\">" + data[0] + "</option>"); 
         //alert(data[0]); 
        } 
        else 
        { 
         // Select the nonunique value by emptying it and appending 
         $('#'+Classofentry).empty("<option value=''selected=\"selected\">" + data[0] + "</option>").append("<option value='" + data[0] + "'selected=\"selected\">" + data[0] + "</option>"); 
         //alert(data[1]); 
        } 

        alert(data[1]);      
        //alert('Success!'); 

        } 

        function errorHandler() 
        { 
         //alert('Error with AJAX!'); 
         alert(data[0]); 
        } 

        $('#add-new-submit').unbind('click'); // This fixes the problem for multiple entries 
        $('#add-new').modal('toggle');      

       }); 
     }); 

PHP:

public function change_options() 
{ 
    # Receives and sends back user-entered new option and adds it to database 

    # Get strings from ajax in view 
    $value = $_POST['new_option']; 
    $value_class = $_POST['new_option_class']; 

    #Make array to send to model 
    $value_array = array('Options' => $value); 
    $unique = true; 
    echo json_encode(array($value, $unique));   
} 

在控制檯中我得到:沒有定義的數據:的ReferenceError。我花了幾天的時間研究邏輯來確定$ unique,現在ajax將無法正常工作,即使當我將它剝離到它的骨頭時也是如此。這是怎麼回事?

+0

拋出一個調試器上,它是在抱怨線數據沒有被定義,並從那裏工作 –

+1

'成功:成功(數據)'? –

+0

爲什麼你在點擊功能中有一個點擊功能,只是好奇 – Ohgodwhy

回答

0

我發佈的代碼應該已經工作。我發現了這個問題,而且沒有用ajax,而是把錯誤的東西傳給模型。這裏的工作代碼,用邏輯來確定$獨特:

(順便說一句,這解決了Form Validation in Codeigniter: using set_rules to check text input from a modal window提出不使用表單驗證的問題):

public function change_options() 
{ 
    # Receives and sends back user-entered new option and adds it to database 

    # Get strings from ajax in view 
    $value = $_POST['new_option']; 
    $value_class = $_POST['new_option_class']; 
    //print_r($value_class); die; 

    #Make array to send to model 
    $value_array = array('Options' => $value); 

    $unique = true; 

    $this->load->model('data_model'); 
    $elements = $this->data_model->get_options($value_class); 

    foreach ($elements as $element) 
    { 
     if (preg_match("/$element/", $value, $matches)) 
     {    
      $unique = false; 
      break; 
     } 
    } 

    # Add new option to dropdown in database 
    if ($unique) {$this->data_model->add_option($value_class, $value_array);} 

    //echo $value; 

    echo json_encode(array($value, $unique));   
}