Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 702
xuzhijian Avatar asked Nov 05 '22 12:11

xuzhijian


1 Answers

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.

like image 181
Elisha Sterngold Avatar answered Nov 09 '22 11:11

Elisha Sterngold