簡體   English   中英

XHR Post用於保存document.body JS

[英]XHR Post for saving document.body JS

我需要將document.body.innerHTML發布到我域的代理服務器上。 PM2正在運行,在使用GET之前,它看起來像:

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

現在,由於信件數量有限,我需要郵寄給我們。 如何將其轉換為POST?

首先確保接收服務器方法接受發布,如果要在xhr中發送數據,您可以這樣做

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

或者,如果服務器接受json對象,則可以使用xhr.send(document.getElementById('#form).serialize());

您應該將其轉換為:

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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM