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

血红的双手。 提交于 2019-12-21 05:11:11

问题


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:


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.



来源:https://stackoverflow.com/questions/7925260/how-to-use-iframe-to-cross-domain-post-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!