How to use iframe to (cross-domain) post request ?

2019-03-30 01:35发布

I want to do a post cross-domain request , I use a form which targeted a iframe to submit the request.

var iframe = document.createElement("iframe");

var uniqueString = "CHANGE_THIS_TO_SOME_UNIQUE_STRING";

document.body.appendChild(iframe);

iframe.style.display = "none";

iframe.contentWindow.name = uniqueString;


var form = document.createElement("form");

form.target = uniqueString;

form.action = myUrl;

form.method = "POST";


// repeat for each parameter

var input = document.createElement("input");

input.type = "hidden";

input.name = "setting";

input.value = params;

form.appendChild(input);

document.body.appendChild(form);

form.submit();

iframe.onload = iframe.onreadystatechange = function(){

if(this.readyState && this.readyState!="complete") return ;

else{                                                                            
       alert("haha");                                                
}
};

The Chrome shows iframe has receive the returned data from remote url, but i cannot get the iframe content using Javascript ? Do you guys have any advices or solutions ?

1条回答
对你真心纯属浪费
2楼-- · 2019-03-30 02:05

You should add a parameter to the form with a GUID. There server should save in the session the GUID with the specific answers. After that you send the form you call the server via JSONP with the GUID that you used in the server and the server should return the asnwers that it saved in the session.

查看更多
登录 后发表回答