2013-07-21 28 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));   
}