$ http - Angular message parameters and get json from PHP

Im new to Angular and even newer using $ http. What I cannot get is the following:

  • post parameters using $ http (required parameters for PHP to make the call)
  • Get JSON as an answer to this call

This is what I got so far:

$ http call:

var deferred = $q.defer();
var parametres = $.param({ nomWS: ws, servidor: ip, query: param, idCamping: dadesCamping[0], forceRenew: renew});       

var url = './api/functions.php?function=callWS_JSON'; 

$http({
    method: 'POST',
    url: url,
    data: parametres,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data) {            
    console.log('success');
    deferred.resolve(data);
}); 

That php that received the call should receive the content using the parameters sent via $ _POST [] and get json using:

$fileContents = file_get_contents($url);        
echo $fileContents;

If I set the parameters manually in a PHP script, it gives a valid JSON string, but when I echo it to extract it into javascript (angular) code, I get the following error in the Chrome console

SyntaxError: Unexpected token &
at Object.parse (native)
at Vb (http://domain/lib/angular/angular.js:14:208)
at e.defaults.transformResponse (http://domain/lib/angular/angular.js:64:454)
at http://domain/lib/angular/angular.js:64:215
at Array.forEach (native)
at r (http://domain/lib/angular/angular.js:7:280)
at pc (http://domain/lib/angular/angular.js:64:197)
at c (http://domain/lib/angular/angular.js:65:400)
at C (http://domain/lib/angular/angular.js:94:187)
at http://domain/lib/angular/angular.js:95:350

If I repeat something before the content echo, I don't get an error, and the console displays the following:

test
{"clientesPresentes": {"cliente":...

(Cut out the conclusion, but the idea is clear)

, SyntaxError: Unexpected token & . , . json- , . .

PD: header('Content-type: application/json'); php , SyntaxError: Unexpected token &

!

+4
2

?

.

var $scope.someData = {
   'param1': 12,
   'param2': 33
};

//someData var can also be set inside a form

<input type="text" ng-model="somedata.param1" >

//post and get available
$http.post('url.php' , $scope.someData)
    .success(function(res){
      ;
             })
    .error(function(error){
      ;
    });

php

$data = file_get_contents("php://input");
echo $data->param1;
0

, JSON .

0

Source: https://habr.com/ru/post/1544008/


All Articles