单击按钮上的ajax

时间:2016-12-27 06:42:40

标签: ajax codeigniter

//在下面的代码输出上发生的是Undefined index:policy,如何在控制器上获取变量策略的值

// product_view.php

    $(".product").on('change', function(){ 
    var policy= "gggggggggggg";
    var base_url = window.location.origin;
//alert(policy);
  $.ajax( {
  type: "POST",
  url: base_url+"/sales/index.php/Add_AMC/Insurance_chg",
  data: policy,
  success: function( response ) {
    alert(response);
  }
});
});

//Add_amc.php controller
//function Insurance_chg(){
        $product=$_POST['policy'];
        echo $product;die();
    }

2 个答案:

答案 0 :(得分:1)

问题可能在于发送数据....

     $(".product").on('change', function(){ 
    var policy= "gggggggggggg";
    //var base_url = 'window.location.origin';
//alert(policy);
  $.ajax( {
  type: "POST",
   url:<?php echo base_url('sales/index.php/Add_AMC/Insurance_chg');?>,
  data: {policy:policy},
  success: function( response ) {
    alert(response);
  }
});
});

或者您可以将数据发送为..

data:'policy='+policy,

在codeigniter中,你可以通过加载base_url()助手来使用url。所以不需要像这样定义base_url ...... 所以改变

url: base_url+"/sales/index.php/Add_AMC/Insurance_chg",

url:<?php echo base_url('sales/index.php/Add_AMC/Insurance_chg');?>,

答案 1 :(得分:1)

试试此代码

$(".product").on('change', function(){ 
    var policy= "gggggggggggg";
    var base_url = window.location.origin;
    var var_data = "policy="+ policy;
  $.ajax( {
     type: "POST",
     url: base_url+"/sales/index.php/Add_AMC/Insurance_chg",
     data: var_data,
     success: function( response ) {
       alert(response);
     }
  });
});