【问题标题】:CodeIgniter Passing POST data from RestClient to RestServer API("SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data")CodeIgniter 将 POST 数据从 RestClient 传递到 RestServer API(“SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data”)
【发布时间】:2018-02-03 02:52:17
【问题描述】:

我花了一整天的时间试图找出这个问题。在这里发布这个问题是我最后的希望。我希望有人可以帮助我继续从事我的第一份工作。

SyntaxError:JSON.parse:JSON 数据的第 2 行第 1 列出现意外字符

所以,当直接将数据从我的视图直接传递到 RestServer 时,POST 可以正常工作。但是,RESTServer API 无法找到从 RestClient 发送的 POST 数据。

我的例子

<?php

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

require APPPATH . 'libraries/REST_Controller.php';



class Example extends REST_Controller {

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

        $this->methods['get']['limit'] = 500; 
        $this->methods['post']['limit'] = 200;
        $this->methods['delete']['limit'] = 50; 
    }



    public function post()
    {

            $message = array(
                'id' => null,
                'name' => $this->input->post('name'),
                'message' => $this->input->post('email')
            ); 

        $this->set_response($message); 
    }


}

【问题讨论】:

  • 尝试使用不同的名称,例如index_post()Docs.
  • 我们可以更改但显示以下错误
  • status false 错误“未知方法”

标签: codeigniter jira-rest-api


【解决方案1】:

首先:,尝试将您的 POST 函数从 post() 重命名为 index_post(),这只是一个有助于 SOLID 原则的标准。

第二: 您的控制器中是否存在您将use CI_Controller 命名空间添加到REST_Controller.php 文件中的位置,因为它不在Example 控制器中;只需 REMOVE 该命名空间。以下 3 个要求应该适合您。

require(APPPATH . 'libraries/REST_Controller.php');
use Restserver\Libraries\REST_Controller;
require (APPPATH . 'libraries/Format.php');

class Example extends REST_Controller { 
... 
}

【讨论】:

    猜你喜欢
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 2018-08-24
    • 1970-01-01
    • 2014-09-01
    相关资源
    最近更新 更多