0

我告诉你,让 AJAX 工作是一种痛苦!我花了很长时间才得到一个简单的字符串,然后我得到了一个 json 数组,感觉很好,现在我试着做一些调整,又把整个事情弄坏了。为什么会出现 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 将无法工作,即使我将其剥离为裸露的骨头也是如此。这是怎么回事?

4

1 回答 1

0

我发布的代码应该可以工作。我发现了问题,不是 ajax,而是我将错误的东西传递给模型。这是工作代码,其中包含确定 $unique 的逻辑:

(顺便说一句,这解决了Codeigniter 中的表单验证中提出的问题:使用 set_rules 来检查来自模式窗口的文本输入而不使用表单验证):

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));           
}
于 2013-07-21T20:25:41.970 回答