如何使用iframe(跨网域)发布要求? [英] How to use iframe to (cross-domain) post request ?

查看:153
本文介绍了如何使用iframe(跨网域)发布要求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要执行后跨网域请求,我使用以iframe为目标的表单提交请求。

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

Chrome显示iframe已收到远端网址传回的资料,但无法取得iframe内容Javascript?

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 ?

推荐答案

您应该使用GUID向表单中添加参数。有服务器应该在会话中保存具有特定答案的GUID。
之后,您发送表单,通过JSONP使用您在服务器中使用的GUID调用服务器,服务器应该返回它保存在会话中的asnwers。

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.

这篇关于如何使用iframe(跨网域)发布要求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆