【问题标题】:Codeigniter Not allowing to post data in REST API post method via url. it's showing error like unknown methodCodeigniter 不允许通过 url 在 REST API 发布方法中发布数据。它显示未知方法之类的错误
【发布时间】:2017-04-20 07:14:58
【问题描述】:

我正在尝试通过像

这样的 url 发送数据

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

但它显示错误消息:

{"status":false,"error":"未知方法"}

当我使用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; 
         } 
    } 
}

【问题讨论】:

  • 您能否发布您认为应该运行的代码,以及任何会导致它返回“未知方法”响应的内容?
  • 我的代码已经填好了,你可以检查一下吗? @gabe3886
  • 您的Society 控制器是直接存储在controllers 文件夹中还是在子文件夹中?另外,您需要对参数进行 URL 编码。您的日期参数有 /,它也用作 URL 分隔符。
  • 存储在控制器中的社会没有子文件夹。如何在参数中编码 URL?我是 codeigniter 中 REST API 领域开发的新手。 @希卡罗

标签: php codeigniter api codeigniter-restserver


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    请参考此帖How do I manually fire HTTP POST requests with Firefox or Chrome?

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

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

    【讨论】:

    • 谢谢兄弟,Content-Type 有问题。
    猜你喜欢
    • 1970-01-01
    • 2015-09-19
    • 2015-08-25
    • 2015-11-25
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    相关资源
    最近更新 更多