0

Hi I am trying to send an append an array of string to a file on my server.

The array has this format:

["Channel", "Tao", "Taco", "", "La", "male", "0", "81.6", "fd", "fd", "fd", "fd", "0", "ci.png", "is", "1", "", "", "20160204_145319"]

The server file is a .jsonp file:

callback([
["brand" , "model"     , "modelStrict"     , "modelSpec"     , "name"    , "gender" , "size"  , "weight" , "length"   , "width"     , "thick"    , "volume" , "fun" , "imageName"      , "photocredits"   , "pertinence" , "year" , "winning", "timestamp"]       ,
["Channel", "To", "Taco", "", "La", "male", "0", "81.6", "fd", "fd", "fd", "fd", "0", "ci.png", "cis", "1", "", "", "20160204_145319"],
["Channel", "Tco", "Tac", "", "La", "male", "0", "81.6", "fd", "fd", "fd", "fd", "0", "ci.png", "cis", "1", "", "", "20160204_145319"],
["Channel", "Taco", "Taco", "", "La", "male", "0", "81.6", "fd", "fd", "fd", "fd", "0", "i.png", "is", "1", "", "", "20160204_145319"],
["Channel", "Taco", "Taco", "", "La", "male", "0", "81.6", "fd", "fd", "fd", "f", "0", "i.png", "cis", "1", "", "", "20160204_145319"]

]);

For now the code I am using is as follow, but it doesn't work, if I try to print the $_POST object in php I get an empty array. Can you help me clean this code ?

Thanks

service.js:

.service('sendToServer', [
        '$http',
        function ($http) {
            'use strict';

            this.f = function (dataToSend) {
                return $http({
                    url: 'http://login:pwd@boardl.com/app/appendToDb.php',
                    method: "POST",
                    data: dataToSend,
                    headers : {
                         'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
                    }
                });
            };
        }
    ])

controller.js:

 sendToServer.f(array).success(handleSuccess).error(handleError);

appendToDb.php on the server:

<?php
//header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST");
header("Access-Control-Allow-Headers: *");

if($_POST) {
  $data[] = $_POST['data'];
  $inp = file_get_contents('../../app/db.jsonp');
  $tempArray = json_decode($inp, true);
  array_push($tempArray, $data);
  $jsonData = json_encode($tempArray);
  file_put_contents('results.json', $jsonData);
}
?>
Louis
  • 2,548
  • 10
  • 63
  • 120

0 Answers0