我想張貼在WordPress的通過我的API應用程序離子的註釋,但它說,錯誤401(非法),這意味着WordPress的API認爲我沒有登錄..beforesend的打字稿相當於在Javascript
我的代碼:
postComment(params?: any) {
let nonce = localStorage.getItem('nonce');
let seq = this.api.post('wp', 'comments', "post="+ params.post+ "&author_name="+params.author_name+"&author="+params.author+"&[email protected]&content=something").share();
seq
.map(res => res.json())
.subscribe(res => {
}, err => {
console.error('ERROR', err);
});
return seq;
}
在我的研究是說我需要發送的隨機數,我可以做這樣的:
.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) {
});
如何在打字稿中做到這一點?如何在打字稿中放入beforeSend函數xhr.setRequestHeader?
只要是明確的 - 這是一個角度與jQuery的區別,而不是一個打字稿與JavaScript的差異, –