2017-06-21 77 views
0

我需要將document.body.innerHTML發佈到我的域的代理。目前正在PM2和之前我用GET,它看上去像:XHR後保存document.body JS

var xhr = new XMLHttpRequest(); 
xhr.open('GET', 'https://domain.ru/pmproxy?info='+document.body.innerHTML+'&location='+document.location+1); 
xhr.send(); 

現在,因爲有信,我需要我們POST的限制。我怎樣才能把它變成POST?

+0

通過用''POST'替換'GET'。另外,在你的innerHTML由於包含一個特殊字符而出錯的時候'encodeURIComponent'可能是一個好主意。 – Watilin

回答

0

首先確保接收服務器方法接受後,如果您要發送的XHR POST數據,你可以像這樣

xhr.send("info='+document.body.innerHTML+'&location='+document.location+1")

,或者如果服務器接受一個JSON對象,那麼你可以使用xhr.send(document.getElementById('#form).serialize());

-1

你應該將其轉換爲這樣的:

var xhr = new XMLHttpRequest(); 
xhr.open('post', 'https://domain.ru/pmproxy'); 
xhr.send('info='+document.body.innerHTML+'&location='+document.location+1); 

另外,我建議你收拾這兩個參數,所以你可避免的問題,在未來:

xhr.send('info='+encodeURIComponent(document.body.innerHTML)+'&location='+encodeURIComponent(document.location+1)); 

而且,我真的不知道爲什麼你使用document.location + 1,但你可以使用document.location.href並保存+1讓你的代碼看起來更清潔:

xhr.send('info='+encodeURIComponent(document.body.innerHTML)+'&location='+encodeURIComponent(document.location.href));