2016-02-13 52 views
1

我想在WP-API中使用angular js在wordpress的文章中添加新评论。这是我的功能:如何使用AngularJS在wp-api中添加新评论?

WPService.setComment = function(comment) { 
    var data = [ 
     { 
      'author_email' : comment.email, 
      'author_name' : comment.name, 
      'author_url' : comment.url, 
      'content'  : comment.body, 
      'post'   : comment.post 
     } 
    ]; 
    return $http.post('wp-json/wp/v2/comments', data[0]).success(function(res, status, header) { 
     console.log('comment posted...!'); 
    }).error(function(err) { 
     console.error(err); 
    }); 
} 

但它不能正常工作。我如何以这种方式添加新评论?

UPD#1似乎角度无法进入错误块并返回错误之前。这些都是错误的:

SyntaxError: Unexpected token < 
at Object.parse (native) 
at fromJson (http://localhost/wordpress/wp-content/themes/theme1/bower_components/angular/angular.js?ver=4.4.2:1271:14) 
at defaultHttpResponseTransform (http://localhost/wordpress/wp-content/themes/theme1/bower_components/angular/angular.js?ver=4.4.2:9460:16) 
at http://localhost/wordpress/wp-content/themes/theme1/bower_components/angular/angular.js?ver=4.4.2:9551:12 
at forEach (http://localhost/wordpress/wp-content/themes/theme1/bower_components/angular/angular.js?ver=4.4.2:340:20) 
at transformData (http://localhost/wordpress/wp-content/themes/theme1/bower_components/angular/angular.js?ver=4.4.2:9550:3) 
at transformResponse (http://localhost/wordpress/wp-content/themes/theme1/bower_components/angular/angular.js?ver=4.4.2:10319:21) 
at processQueue (http://localhost/wordpress/wp-content/themes/theme1/bower_components/angular/angular.js?ver=4.4.2:14792:28) 
at http://localhost/wordpress/wp-content/themes/theme1/bower_components/angular/angular.js?ver=4.4.2:14808:27 
at Scope.$eval (http://localhost/wordpress/wp-content/themes/theme1/bower_components/angular/angular.js?ver=4.4.2:16052:28) 
+0

你有没有得到任何错误? – Minato

+1

我想你的'wp-api'代码没有返回正确的响应。 – Minato

+0

我认为你是对的。因为我一个一个地注释掉了参数,并且当我发送'post'参数时我得到错误。我想我应该在github项目上报告这一点。 – bobsilon

回答

1

由于WP-API's github issues page,这是这将是在2.0版测试版13 对于下一个测试版发布之前,治标不治本的决心,我们可以应用80dcacf提交上class-wp-rest-comments-controller.php文件的插件中的错误。