1

So, I have this really simple snippet in a controller where I get data from an external file using $http.get, and it works great! But when I use $http.post, I get a "syntaxerror: unexpected token < at object.parse(native)" in my console.

I've included both versions below with the working $http.get commented out.

var blogCtrl = angular.module('blogCtrl', []);

blogCtrl.controller('articleCtrl', ['$scope', '$routeParams', '$http',
function($scope, $routeParams, $http) {
    //$http.get('/angular_blog/assets/php/ajaxRequests.php?action=fetchSingleArticle&permalink='+$routeParams.articlePermaLink)
    $http.post('/angular_blog/assets/php/ajaxRequests.php', {action: 'fetchSingleArticle', permalink: $routeParams.articlePermaLink})
    .success(function(data) {
        $scope.articleTitle = data.articleTitle;
        $scope.articleAuthor = data.articleAuthor;
        $scope.articleContent = data.articleContent;
        $scope.publishDate = data.publishDate;
        $scope.category = data.category;
        $scope.categoryPermaLink = data.categoryPermaLink;
    });
}]);

I already tried the suggestion in this question, but it gave the same result.

Community
  • 1
  • 1
Brian Emilius
  • 717
  • 1
  • 8
  • 31
  • Have you tried this :http://stackoverflow.com/questions/19254029/angularjs-http-post-does-not-send-data – Shubham Nigam Jul 28 '15 at 06:34
  • try setting content-type. – govindpatel Jul 28 '15 at 06:37
  • I think your json is not proper. So angular is not able to parse. May be that is why you are getting syntax error. Can you provide value of this part : `$routeParams.articlePermaLink`. Thank you – govindpatel Jul 28 '15 at 06:44
  • @Shubham, I'm looking into that, thanks! govindpatel: $routeParams.articlePermaLink = "something", but that's not the issue, cause if I uncomment the get-line and outcomment the post-line (and change $_POST to $_GET in my PHP), it works just fine. I'm looking into changing the way my PHP interprets the json data send by my controller. – Brian Emilius Jul 28 '15 at 09:40

1 Answers1

0

I figured it was better to make my PHP-file understand a json post request, so instead of trying to grab strings with $_POST['string'], do this:

$obj = json_decode(file_get_contents('php://input'));

and then when I want to use strings from the request:

$obj->{'string'}
Brian Emilius
  • 717
  • 1
  • 8
  • 31