-1

I have the following function below which I am trying to re-write using $http provider. The documentation shows so many different ways of doing this and I cant get it right. Here is the function:

function Ingest(Filename, ID, baseUrl, logger){       
  var url = baseUrl + '/php/' + 'Ingest.php';                                                                
  var dataString = 'Filename=' + encodeURIComponent(Filename) + '&ID=' + encodeURIComponent(ID);

  $.ajax({
    type: "POST",
    url: url,
    async: true,
    cache: false,
    data: dataString,
    success: function(results){                  
        logger.success('Ingestion process has been finished.', '', 'Success');                                                                                 
    }
    //fail
    , error: function (jqXHR, textStatus, errorThrown){
        alert("error:\r\n" + errorThrown);
    }            
  });
}

and here is a sample of $http code:

$http({
method: 'POST',
url: config.appBaseUrl + '/php/' + 'Ingest.php',
data: { ID: encodeURIComponent(ID), Filename: encodeURIComponent(Filename) }

}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
}, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});

Thank you

Max
  • 1,289
  • 3
  • 26
  • 50
  • what's the error in the code? – jkris Jan 18 '16 at 15:47
  • In your jquery example you trying to `post` data, in your angular example you are trying to `get` data. – Daniel Lizik Jan 18 '16 at 15:50
  • Well, one request is using `GET` the other one is using `POST` which one is it? – MinusFour Jan 18 '16 at 15:51
  • sorry, it was a typo, the error is that the PHP page is not receiving any data on the other side. I am using these 2 lines to grab the POST data: $id = $_POST['ID']; $file = $_POST['Filename']; – Max Jan 18 '16 at 16:41
  • Found my answer here: http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined thanks – Max Jan 18 '16 at 17:14

1 Answers1

1

In the 1st sample you do a post, in the second a get.

You can just use shortcut method provided by $http.

$http.post( config.appBaseUrl + '/php/' + 'Ingest.php',  Filename: encodeURIComponent(Filename), ID: encodeURIComponent(ID)).then(function(response){

}, function(rejection){

});

If you want to set some specific configuration for the $http (headers,...) use the 3rd argument of the functions.

Note that shortcut post/put have a second argument for request body, 3rd for configurations.Delete and get does not have request body argument so configuration are the 2nd argument of the function.

Walfrat
  • 5,363
  • 1
  • 16
  • 35