【问题标题】:Hitting directly a controller in codeigniter and getting json response直接在codeigniter中点击控制器并获得json响应
【发布时间】:2018-04-27 01:49:14
【问题描述】:

我在我的 codeigniter 控制器中这样做

public function getprice($origin,$destination){
    $this->load->model("flights");
    $query['data']=$this->flights->giveprice($origin,$destination);  //model gives fine result
     $this->output
    ->set_content_type('application/json')
    ->set_output(json_encode(array('foo' => $query['data'])));   //data is printed correctly


   }

我已经用确切的这个网址点击了这个网址:-

localhost/codeIgniter/index.php/booking/getprice?origin="Karachi"&destination="Lahore"

我得到这个作为回应:-

 <div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>An uncaught Exception was encountered</h4>
<p>Type: ArgumentCountError</p>
<p>Message: Too few arguments to function Booking::getprice(), 0 passed in /opt/lampp/htdocs/codeIgniter/system/core/CodeIgniter.php on line 532 and exactly 2 expected</p>
<p>Filename: /opt/lampp/htdocs/codeIgniter/application/controllers/Booking.php</p>
<p>Line Number: 22</p>
<p>Backtrace:</p>
<p style="margin-left:10px">
        File: /opt/lampp/htdocs/codeIgniter/index.php
    <br />
        Line: 315
    <br />
        Function: require_once
</p>

我的另一个不带参数的函数运行良好,但是当涉及参数(“GET”)时,它说“给出的论点太少”,我提供了 2 个参数。请纠正我做错的地方。

【问题讨论】:

    标签: php json codeigniter get


    【解决方案1】:

    您有使用获取参数的调用函数。 但是在codeigniter中调用不同于普通的函数调用。 示例:http://localhost/project/main/getproduct/24/45

    看到这个 Passing multiple variables in URL using codeigniter

    【讨论】:

    • 是的,但是如果我想像这样调用 ....localhost/codeIgniter/index.php/booking/getprice?origin=Karachi&destination=Lahore 而不是 ..localhost/codeIgniter/index。 php/booking/getprice/卡拉奇/拉合尔???
    • 比在控制器中你需要给 $GET['origin'] 但是当你传递参数时你可以很容易地给 $origin
    • 它给出了“未定义的变量 $GET”
    • 哦抱歉是 $_GET
    【解决方案2】:

    您有使用获取参数的调用函数。 但是在给定参数时调用codeigniter与普通函数调用不同。 例子 : localhost/codeIgniter/index.php/booking/getprice/卡拉奇/拉合尔

    看到这个:Passing multiple variables in URL using codeigniter

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 2016-11-05
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      • 2013-10-02
      • 2015-03-22
      • 2018-04-01
      • 2016-11-27
      相关资源
      最近更新 更多