Send Data To Different Domain
I need to send a fairly small amount of data (~215characters) from Domain A to Domain B using only JavaScript (No JQuery, etc). I have full control over Domain B, so how the data gets there really isn't as important as how it's sent (Only using JavaScript) and there is zero need for Domain B to send anything back开发者_运维问答 to Domain A.
I believe one solution to this problem is having Domain A request an image file with a query string from Domain B. On Domain B I can then capture the request and parse the data in the query string. In fact, I believe this is how analytics works.
Possible other solutions?
Image is not ideal as modern browsers might block it as part of privacy, think I saw it somewhere.
Personally I will create a form
on the fly and submit it to hidden frame:
var _TargetPage = "http://www.domainB.com/page.php";
function SendData(strData) {
var oFrame = document.getElementById("HiddenFrame");
if (!oFrame) {
oFrame = document.createElement("iframe");
oFrame.id = "HiddenFrame";
oFrame.name = "HiddenFrame";
oFrame.style.display = "none";
document.body.appendChild(oFrame);
}
var oForm = document.getElementById("HiddenForm");
if (!oForm) {
oForm = document.createElement("form");
oForm.id = "HiddenForm";
oForm.method = "POST";
oForm.action = _TargetPage;
oForm.target = "HiddenFrame";
document.body.appendChild(oForm);
var oInput = document.createElement("input");
oInput.type = "hidden";
oInput.name = "HiddenInput";
oForm.appendChild(oInput);
}
oForm.elements["HiddenInput"].value = strData;
oForm.submit();
}
This way you don't have to mess with URL encoding and you have more control over things, plus it's harder to spoof. (Browsing to certain URL alone won't be enough)
精彩评论