doujiejujixi27244 2016-08-31 10:37
浏览 79

Javascript - Json使用codeigniter从表中获取值

I am new in ajax and json. and in my select option i have list of products there. and if i press any of the product it will show its price and the supplier. but what happened to my code is it shows both product and supplier but not in there specific fields. Here is the Example
The 550.00 is the price and asdadsadsadsadsadsad there is the name of the supplier. so the output must be in their specific fields.


    var prodid = $("#prod-names option:selected").attr("value");
        url: "http://localhost:800/client_ayos/administrator/createpromoajax/"+prodid,
        type: "POST",

        success: function(data){
            var json = JSON.parse(data);

            $("#orig-price").val(json); //val =  value of #orig-price
            $("#supplier").val(json); //val =  value of #orig-price

        error: function (jqXHR, textStatus, errorThrown){
                    alert('Error !');


    public function createpromoajax($id){
    //echo json_encode($this->uri->segment(3));
    $data['productid'] = $this->AdminModel->get_prodid($id);

    $price = $data['productid']->price;
    $supplier = $data['productid']->supplier;

    $ajaxproduct = array(

    echo json_encode($ajaxproduct);


PS: I've also encountering this kind of error but still it shows the correct output but not in the specific fields

VM13100:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • donglu5728 2016-08-31 11:02

    i think there is a lot to simplify your controller and js structure try something like that

    here is the php part

    public function createpromoajax($id)
        $objProduct = $this->AdminModel->get_prodid($id);
        header('Content-Type: application/json');
        echo json_encode($objProduct);

    and your javascript part

        var prodid = $("#prod-names option:selected").attr("value");
        var url = "http://localhost:800/client_ayos/administrator/createpromoajax/"+prodid;
        $.getJSON( url, function(data) {
            $("#supplier").val(data.supplier); //val =  value of #orig-price

    i'm not sure what it means "unexptected token..." but i think you already output some html before the createpromoajax function - in order to verify this you could try to call your function directly and see what your browser gets as output.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验
  • ¥15 为什么shp文件会有这种小方块?
  • ¥15 ecplise在连接数据库时显示加载驱动成功但是数据库连接失败
  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 Multisim红外倒车雷达仿真中距离问题
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应