0

我正在尝试通过 url 发送数据,例如

http://localhost/api/index.php/society/?date=20/04/2017&s_name=ssr

但它显示错误消息:

{“状态”:假,“错误”:“未知方法”}

当我使用postmanadvance rest client应用程序进行测试时,它工作正常,数据正在发布和更新数据库。

网址:http://localhost/api/index.php/society

我的控制器……

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

require_once APPPATH . 'libraries/REST_Controller.php';

use Restserver\Libraries\REST_Controller;

class Society extends REST_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->database(); 
        $this->load->model('Society_model');
    }

    public function index_get()
    {
        $data = $this->Society_model->society_get();
        $this->response($data);
    }

    public function index_post()
    {

        $data = array( 
            'date' =>$this->post('date'),
            'society_name' => $this->post('s_name'), 
            'society_group' => $this->post('g_name'),
            'contact_name' => $this->post('c_name'),
            'mobile_no' => $this->post('mobile'),
            'address' => $this->post('address'),
            'city' => $this->post('city'),
            'state' => $this->post('state'),
            'pincode' => $this->post('pincode'),
            'email_id' =>$this->post('email')
           ); 

         $this->Society_model->insert($data); 

         $this->set_response($data, REST_Controller::HTTP_CREATED);
    }
}

我的模特……

<?php

class Society_model extends CI_Model
{

    function __construct()
    {
        parent::__construct();  
    }

    public function society_get(){
        $this->db->select('*');
        $this->db->from('society_contact');
        $result = $this->db->get();
        return $result->result();
    }

    public function insert($data) 
    { 
         if ($this->db->insert("society_contact", $data)) 
         { 
            return true; 
         } 
    } 
}
4

2 回答 2

0

您不能通过浏览器使用 POST 方法。您必须使用 POSTMAN 或其他 REST 客户端。浏览器中的 URL 只允许您获取信息。

请参阅这篇文章如何使用 Firefox 或 Chrome 手动触发 HTTP POST 请求?

还有你的 URL http://localhost/api/index.php/society/?date=20/04/2017&s_name=ssr

这是不正确的,因为当您添加时?something=something,这是一个参数,如果您添加/这是 url 的单独部分。这就是你得到Unknow Method的原因。

于 2017-06-08T16:48:32.333 回答
0

Society是您的模型函数名称..通过索引名称 http://localhost/api/index.php/index/?date=20/04/2017&s_name=ssr 调用它

于 2017-04-20T14:12:28.033 回答