我的代码jquery ajax jason有什么问题

时间:2018-03-08 04:50:48

标签: jquery

我正在尝试在输入id时自动填写表单中的某些字段,但它无效。

这是我的控制器功能

public function payment()
{
    $crud = new RMCL_grocery_CRUD();
    //$crud->set_theme('bootstrap-v4');
    $crud->set_table('wp_rmcl_pub_payment');
    $crud->set_subject("_payment");
    $crud->callback_add_field('payment_type',array($this,'add_field_callback_1'));
    $crud->callback_edit_field('payment_type',array($this,'add_field_callback_1'));
    $crud->set_js('resources/js/sample.js');
    $name=$this->input->post('subscriber_id');
    $connect=mysqli_connect('localhost','root','123456','armdb');
    $result=mysqli_query($connect,"select * from wp_rmcl_pub_subscription where 
    subscriber_id='$name'");
    $response = array();
    while($row=mysqli_fetch_array($result))
    {
        $subscription_type= $row['subscription_type'];
        $magazine_type= $row['magazine_type'];
        $response[] = array("subscription_type" => 
        $subscription_type,"magazine_type" => $magazine_type);
    }
    echo json_encode($response);
    $output = $crud->render();
    $this->addData('output', $output);
    $this->render('app/crud_view');   
}

这是我的jquery

$('#field-subscriber_id').change(function(){
    var subscriber_id = $('#field-subscriber_id').val();
    event.preventDefault();
    $.ajax({
        type: 'post',
        dataType:JASON,
        data: {ajax: 1,subscriber_id: subscriber_id},
        success: function(response){
            $('#field-subscription_type').val('' + response.magazine_type);
            $('#field-magazine_type').val('' + response.subscription_type);
        }
    });
});

我正在尝试在输入id时自动填写表单中的某些字段,但它无效。

这是我的控制器功能 它没有显示任何内容。

1 个答案:

答案 0 :(得分:0)

dataType没有JASON。将其替换为'json'

$('#field-subscriber_id').change(function() {
  var subscriber_id = $('#field-subscriber_id').val();
  event.preventDefault();
  $.ajax({
    type: 'post',
    dataType: 'json',
    data: JSON.stringify({
      ajax: 1,
      subscriber_id: subscriber_id
    }),
    success: function(response) {
      $('#field-subscription_type').val('' + response.magazine_type);
      $('#field-magazine_type').val('' + response.subscription_type);
    }
  });
});