我們這里使用了iframe嵌套form表單POST提交,很簡單,卻能滿足get|post等任何復雜情況的要求;缺點是沒有返回值。
針對需要的地方加上一個iframe嵌套並塞入隱藏form表單,然后獲取登錄表單的值賦予到隱藏form表單提交即可完成跨域POST|get提交。
function smal_send(){
var user = $("#user_name").val();
var pass = $("#password").val();
var form =$("<form action='http://www.demo.com/login.php' method='post'>" +
"<input type='hidden' name='user_name' value=''/> " +
"<input type='hidden' name='password' value=''/> " +
"</form> ");
$( "#SMAL" ).remove();//清空節點
$( "body").append("<iframe id='SMAL' name='SMAL' style='display: none'></iframe>");//載入iframe
(function(){
$( "#SMAL" ).contents().find('body').html(form);//將form表單塞入iframe;
$( "#SMAL" ).contents().find("form input[name='user_name']").val(user);
$( "#SMAL" ).contents().find("form input[name='password']").val(pass);
$( "#SMAL" ).contents().find('form').submit();//提交[ps,這里是提交到demo.com,跨域]
}());
setTimeout(function(){//如果需要可以跳轉到demo.com
window.location.href="http://www.demo.com/index.php";
},100)
}
javascript是要逆天的。嗯嗯,我覺的是。
參考:
1:http://huangqiqing123.iteye.com/blog/1754108
2:這里介紹了基本的跨域提交方式 http://blog.csdn.net/china_skag/article/details/7410918
3:群友討論。
4:發散知識點:http://www.cnblogs.com/yjmyzz/archive/2011/04/26/2029699.html