简体   繁体   English

jQuery post vs angular使用POST方法

[英]jquery post vs angular using POST method

I am trying to serialize a form and send it over to the server using the http POST method... 我正在尝试序列化表单并使用http POST方法将其发送到服务器...

i have the code as follows for jquery .. 我有以下代码的jQuery ..

    var data = $scope.modal.element.find('form').serialize();

    $.ajax({
        type: "POST",
        url: "/user/add.html",
        data: data
    }).success(function(ret){
        $scope.modal.body = $sce.trustAsHtml(ret);
        $http.get("/user/list.json").success(function(data){
            $scope.users = data;
        });
    }); 

the code above works just fine... when i use angular to post the data with the following code... 上面的代码工作正常...当我使用angular使用以下代码发布数据时...

       var data = $scope.modal.element.find('form').serialize();

       $http.post("/user/add.html", data).success(function(html){
            $scope.modal.body = $sce.trustAsHtml(html);
            $http.get("/user/list.json").success(function(data){
                $scope.users = data;
            });
        });

the server freaks out ... and cannot understand any of the variables that are sent over... the server returns an error saying all my fields are empty ... (it cannot parse it) 服务器吓坏了...并且无法理解通过其发送的任何变量...服务器返回错误,提示我所有字段均为空...(无法解析)

i have read some answers where it involves editing the html of the form and use ng-model to get the variables unfortunately i am in liberty of doing that ... 我已经阅读了一些答案,其中涉及编辑表单的html并使用ng-model来获取变量,不幸的是,我可以这样做...

please tell me what the difference between these 2 methods are ... and why one is working and the otherone is not... 请告诉我这两种方法之间的区别是什么...为什么一个起作用而另一个却不起作用...

PS: i would like to use angular to POST the data PS:我想使用角度发布数据

Try doing it like this: 尝试这样做:

   var data = $scope.modal.element.find('form').serialize();

   $http({
        method: 'POST',
        url: "/user/add.html",
        data: data,
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
   }).success(function(html){
        $scope.modal.body = $sce.trustAsHtml(html);
        $http.get("/user/list.json").success(function(data){
            $scope.users = data;
        });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM