This is an old question, but the current REST API indicates that the X-WP-NONCE
header should be set via the beforeSend
callback. The following is directly from the REST API docs.
.ajax( {
url: wpApiSettings.root + 'wp/v2/posts/1',
method: 'POST',
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
},
data:{
'title' : 'Hello Moon'
}
} ).done( function ( response ) {
console.log( response );
} );
You can drop the headers
property by doing the above, and using the contentType
property. That is, if you use the $.ajax
function.
$scope.formData = {}; //stores input from form above
$scope.processForm = function() {
console.log(RestAPI.nonce);
// using $.ajax not $http
$.ajax({
method: 'POST',
url: '/wp-json/wp/v2/posts',
data: $scope.formData,
transformRequest: angular.identity,
contentType: 'multipart/form-data',
beforeSend: function( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', RestAPI.nonce );
},
success: function (result) {
console.log('Success!');
},
error: function () {
console.log('Fail!');
}
});
};