Javascript Jquery post,在新窗口中响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3825078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery post, response in new window
提问by Peter
I have a script that on.DocumentReady posts data to another page. That page responds with some HTML encapsulated in one div tag.
我有一个脚本 on.DocumentReady 将数据发布到另一个页面。该页面响应一些封装在一个 div 标签中的 HTML。
My goal is to have this post response/data open in a new window.
我的目标是在新窗口中打开此帖子响应/数据。
Any hints or clues?
任何提示或线索?
Here is the snippet I created from Dr. Mille's advice.
这是我根据 Mille 博士的建议创建的片段。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var packslip_id = 35592;
var po_no = 0018439;
var box_no = 1;
$.post("https://example.com/barcode/generate", { packing_slip: packslip_id, reference: po_no, total_boxes: box_no},
function (data) {
alert(data);
var win=window.open('about:blank');
with(win.document)
{
open();
write(data);
close();
}
});
});
回答by Dr.Molle
Use the write()-Method of the Popup's document to put your markup there:
使用 Popup 文档的 write()-Method 将您的标记放在那里:
$.post(url, function (data) {
var w = window.open('about:blank');
w.document.open();
w.document.write(data);
w.document.close();
});
回答by Rocklan
Accepted answer doesn't work with "use strict" as the "with" statement throws an error. So instead:
接受的答案不适用于“use strict”,因为“with”语句会引发错误。所以与其:
$.post(url, function (data) {
var w = window.open('about:blank', 'windowname');
w.document.write(data);
w.document.close();
});
Also, make sure 'windowname' doesn't have any spaces in it because that will fail in IE :)
另外,请确保“windowname”中没有任何空格,因为这将在 IE 中失败:)
回答by Dr.Molle
If you dont need a feedback about the requested data and also dont need any interactivity between the opener and the popup, you can post a hidden form into the popup:
如果您不需要有关所请求数据的反馈,也不需要开启器和弹出窗口之间的任何交互,您可以将隐藏表单发布到弹出窗口中:
Example:
例子:
<form method="post" target="popup" id="formID" style="display:none" action="https://example.com/barcode/generate" >
<input type="hidden" name="packing_slip" value="35592" />
<input type="hidden" name="reference" value="0018439" />
<input type="hidden" name="total_boxes" value="1" />
</form>
<script type="text/javascript">
window.open('about:blank','popup','width=300,height=200')
document.getElementById('formID').submit();
</script>
Otherwise you could use jsonp. But this works only, if you have access to the other Server, because you have to modify the response.
否则你可以使用jsonp。但这仅适用于您有权访问其他服务器的情况,因为您必须修改响应。
回答by NickNuke
I did it with an ajax post and then returned using a data url:
我用 ajax 帖子做了,然后使用数据 url 返回:
$(document).ready(function () {
var exportClick = function () {
$.ajax({
url: "/api/test.php",
type: "POST",
dataType: "text",
data: {
action: "getCSV",
filter: "name = 'smith'",
},
success: function(data) {
var w = window.open('data:text/csv;charset=utf-8,' + encodeURIComponent(data));
w.focus();
},
error: function () {
alert('Problem getting data');
},
});
}
});