$http.post not working in AngularJS

后端 未结 4 1600
别那么骄傲
别那么骄傲 2021-01-12 03:09

When I use $http.get my code works, but if I use $http.post, I never get the parameters to the request .php file.

This is Service function:

         


        
相关标签:
4条回答
  • 2021-01-12 03:47

    What if you specify the content-type in the headers, specifically like this:

    $http({
    method: 'POST',
    url: '/data/AJAXRequest.php',
    data: { mydata: 1, abcd: 2 },
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).success(....);
    

    Found comments relating to PHP specifically from this question: AngularJs $http.post() does not send data

    It would seem that Angular sends as application/json by default, which can confuse PHP.

    0 讨论(0)
  • 2021-01-12 03:50

    Post data like this:

    $http.post('/data/AJAXRequest.php', { mydata: 1, abcd: 2 })
    
    0 讨论(0)
  • 2021-01-12 03:50
    $http({
        method: 'POST',
        url: 'http://mtapi.azurewebsites.net/api/sectiontype',
        dataType: 'json',
        data: {SectionTypeId:0, Name: $scope.message},
        headers: { 'Content-Type': 'application/json; charset=UTF-8' }
    }).success(function (data) {
        alert(data);
    }).error(function (data) {
        alert(data);
    });
    

    headers must be added in the end of the array otherwise it won't work.

    0 讨论(0)
  • 2021-01-12 03:55

    I faced the same issue but I found that there is no issue in angular http post method, issue is there where I am trying to get post data.

    $params = json_decode(file_get_contents('php://input'),true);
    

    I used this code to get the posted data from angular and its works like a charm

    0 讨论(0)
提交回复
热议问题