角度.js http.post 美元,Codeigniter 不工作


Angular.js $http.post with Codeigniter not working

我正在尝试使用$http提交新帖子。 它不起作用。我尝试了岸上版本和长版本,都失败了。控制台:"加载资源失败:服务器响应状态为 500(内部服务器错误(">

这是我的代码:

$scope.doAdd = function(){
    $http({
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    url: 'api/addduans',
    method: "POST",   
      })
      .success(function(data) {
        alert('OK');
      });
    }

我的控制器:

function addduans_post()  
    {  
            $id_duan = $this->post('id_duan');  
            $title = $this->post('title');
            $addr = $this->post('addr');
            $dis = $this->post('dis');
            $img_duan = $this->post('img_duan');
        $result = $this->admin_model->add_id_da($id_duan,$title,$addr,$dis,$img_duan);

        if($result === FALSE)  
        {  
            $this->response(array('status' => 'failed'));  
        }  
        else 
        {  
            $this->response(array('status' => 'success'));  
        }  
    }  

我的模型:

public function add_id_da($id_duan,$title,$addr,$dis,$img_duan) 
        {
        $data = array(
           'id_duan' => $id_duan,
           'title' => $title,
           'addr' => $addr,
           'dis' => $dis,
           'img_duan' => $img_duan
        );
        $this->db->insert('duan_test', $data); 
        }

这是我的观点:

<tr>
                <td> <input name='id_duan' style='width: 50px' ng-model='id_duan'/> </td>
                <td> <input name='title' ng-model='title'/> </td>
                <td> <input name= 'addr' ng-model='addr'/>  </td>
                <td> <input  name='dis' style='width: 60px' ng-model='dis'/>    </td>
                <td> <input name='img_duan' ng-model='file_name'/>  </td>
                <td> <a href="" ng-click="doAdd()" class="btn - btn-info">Add</a>   </td>
            </tr>

有人知道如何完成这项工作吗?谢谢!

第 1 步:将输入字段转换为表单。

<form ng-submit='doAdd()'>
<tr>
    <td> <input name='id_duan' style='width: 50px' ng-model='myForm.id_duan'/> </td>
    <td> <input name='title' ng-model='myForm.title'/> </td>
    <td> <input name= 'addr' ng-model='myForm.addr'/>  </td>
    <td> <input  name='dis' style='width: 60px' ng-model='myForm.dis'/>    </td>
    <td> <input name='img_duan' ng-model='myForm.file_name'/>  </td>
    <td> <input type="submit" class="btn - btn-info" value="add"> </td>
</tr>
</form>

第 2 步:提交表单

$scope.formData = {};
$scope.doAdd = function(){
    $http({
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        url: 'api/addduans',
        method: "POST",
        data    : $.param($scope.formData)
    })
    .success(function(data) {
        alert('OK');
    });
}

希望对您有所帮助。你似乎正在从jQuery切换。有关简单的教程,请参阅此处。

我遇到了这样的情况。我使用了自定义序列化服务,如下所示。它可能用于您的问题。

appCommonServiceModule.factory('SerialiazeService', function () {
        function SeriliazeService() {
            this.serialiaze = function (obj, prefix) {
                var str = [];
                for (var p in obj) {
                    var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
                    str.push(typeof v == "object" ? this.seriliaze(v, k) : encodeURIComponent(k) + "=" + encodeURIComponent(v));
                }
                return str.join("&");
            };
        }
        return new SeriliazeService();
    });

它像这样用在 $http.post 中(memberModel 是 JavaScript 对象数据类型(:

$http.post(BASE_URL + 'index.php/signUp', SerialiazeService.serialiaze(memberModel), {responseType: 'json', headers: {'Content-Type': 'application/x-www-form-urlencoded'}})

更改此代码

$http({
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    url: 'api/addduans',
    method: "POST",
    data    : $.param($scope.formData)
})

对此:

var request = $http.post('api/addduans', $.param($scope.formData), {headers : {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}});