【问题标题】:Codeigniter Post data using AngularJSCodeigniter 使用 AngularJS 发布数据
【发布时间】:2017-01-17 13:09:20
【问题描述】:

我已经使用 Codeigniter + AngularJS 构建了 CRUD 应用程序

如何在代码点火器控制器中发布数据

我正在使用这个函数获取所有的 POST 数据

$data = json_decode($this->input->raw_input_stream , TRUE);

但我想要使用这个函数的特定值,但响应为 NULL

$x = $this->input->input_stream('email', TRUE);

还有一个问题是如何应用代码点火器表单验证 对于这个 $data

谢谢你 请帮帮我

【问题讨论】:

    标签: php angularjs codeigniter http-post forms


    【解决方案1】:

    尝试以下方式。

    我假设了您的代码并提供了一个示例,请根据您的需要进行必要的更改。

    Angular Js

    console.log("posting data....");
    $http({
        method: 'POST',
        url: '<?php echo base_url(); ?>user/add',
        headers: {'Content-Type': 'application/json'},
        data: JSON.stringify({name: $scope.name,city:$scope.city})
    }).success(function (data) {
        console.log(data);
        $scope.message = data.status;
    });
    

    控制器动作

    public function add()
    {
       // Here you will get data from angular ajax request in json format so you have to decode that json data you will get object array in $request variable
        $postdata = file_get_contents("php://input");
        $request = json_decode($postdata);
        $name = $request->name;
        $city = $request->city;
        $id = $this->user_model->AddUser($name,$city);
        if($id)
        {
         echo $result = '{"status" : "success"}';
        }else{
         echo $result = '{"status" : "failure"}';
        }
    }
    

    【讨论】:

    • 感谢您的回复,但如何在您的函数中使用 codeigniter 表单验证添加因为服务器端表单验证未应用我正在尝试更多时间请告诉我
    • 您可以在服务器端执行此操作并相应地发送验证成功/失败消息 例如。 $_POST = json_decode(file_get_contents("php://input"), true); $this->form_validation->set_rules($data->name, 'Name', 'trim|required|min_length[3]'); if(!$this->form_validation->run()) { echo $result = '{"errors" : validation_errors()}'; }
    • @Nepster 看看 codeigniter 如何进行验证但是您需要获取发布数据,然后您需要检查验证,然后将结果返回给客户端(角度)并显示该消息。
    • 感谢您的回复,但验证未应用错误出现 First_name 字段为必填项
    • 你遇到了什么问题?
    猜你喜欢
    • 2023-03-18
    • 2014-11-10
    • 1970-01-01
    • 2018-05-28
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 2018-02-06
    相关资源
    最近更新 更多