AngularJs $http.post() does not send data

前端 未结 30 1768
我在风中等你
我在风中等你 2020-11-22 02:51

Could anyone tell me why the following statement does not send the post data to the designated url? The url is called but on the server when I print $_POST - I get an empty

30条回答
  •  盖世英雄少女心
    2020-11-22 03:37

    I know has accepted answer. But, following might help to future readers, if the answer doesn't suit them for any reason.

    Angular doesn't do ajax same as jQuery. While I tried to follow the guide to modify angular $httpprovider , I encountered other problems. E.g. I use codeigniter in which $this->input->is_ajax_request() function always failed (which was written by another programmer and used globally, so cant change) saying this was not real ajax request.

    To solve it, I took help of deferred promise. I tested it in Firefox, and ie9 and it worked.

    I have following function defined outside any of the angular code. This function makes regular jquery ajax call and returns deferred/promise (I'm still learning) object.

    function getjQueryAjax(url, obj){
        return $.ajax({
            type: 'post',
            url: url,
            cache: true,
            data: obj
        });
    }
    

    Then I'm calling it angular code using the following code. Please note that we have to update the $scope manually using $scope.$apply() .

        var data = {
            media: "video",
            scope: "movies"
        };
        var rPromise = getjQueryAjax("myController/getMeTypes" , data);
        rPromise.success(function(response){
            console.log(response);
            $scope.$apply(function(){
                $scope.testData = JSON.parse(response);
                console.log($scope.testData);
            });
        }).error(function(){
            console.log("AJAX failed!");
        });
    

    This may not be the perfect answer, but it allowed me to use jquery ajax calls with angular and allowed me to update the $scope.

提交回复
热议问题