从另一个站点使用jquery提交表单

时间:2015-01-30 05:55:39

标签: javascript jquery

我正在尝试使用jquery提交表单,但它在控制台中给出了错误。

Uncaught SecurityError: Blocked a frame with origin "http://ws1.osfi-bsif.gc.ca" from accessing a frame with origin "http://jsfiddle.net". Protocols, domains, and ports must match

这是我的代码: -

post('http://ws1.osfi-bsif.gc.ca/WebApps/FINDAT/DTIBanks.aspx?T=0&LANG=E', {DTIWebPartManager$gwpDTIBankControl1$DTIBankControl1$institutionTypeCriteria$institutionsDropDownList: 'Z005',DTIWebPartManager$gwpDTIBankControl1$DTIBankControl1$dtiReportCriteria$monthlyDropDownList:'DTI-1',DTIWebPartManager$gwpDTIBankControl1$DTIBankControl1$dtiReportCriteria$monthlyDatesDropDownList:'11 - 2014'});


 function post(path, params, method) {
method = method || "post"; // Set method to post by default if not specified.

// The rest of this code assumes you are not using a library.
// It can be made less wordy if you use one.
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);

for(var key in params) {
    if(params.hasOwnProperty(key)) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
     }
}

document.body.appendChild(form);
form.submit();
}

这是JSFiddle的工作: -

http://jsfiddle.net/kokmut5v/1/

1 个答案:

答案 0 :(得分:0)

问题确实是同源策略,因为表单是从不同的域加载而不是父窗口的域加载的。
我认为您应该将表单放在iframe中并将sandbox="allow-same-origin allow-forms"添加到iframe 有关iframe和沙盒的详细信息,您可以查看herehere