我正在尝试在弹出功能中运行ajax。
基本上,我有一个非常简单的JavaScript函数,我需要在其中运行ajax调用。
为了解释这一点,这是我的简单弹出功能:
if(window.opener && !window.opener.closed) {
}
window.close(); // Closing the popup
这就是我如何在其中运行我的ajax:
if(window.opener && !window.opener.closed) {
var img = $('#img').val();
alert(img);
$.ajax({
url:"folder/page.php",
data:{img:img},
type:"POST",
contentType:"application/x-www-form-urlencoded",
success:function(result)
{
}
});
}
window.close(); // Closing the popup
这就是我所说的:
<a href="#" onClick="window.open('folder/page.php', 'Popup', 'width=640,height=480');">Some Text/a>
当我运行我的代码时,弹出窗口正常打开,但根本没有执行ajax。
我已经在page.php
内找到了这个,以确保发送/发布ajax:
$img = $_POST['img'];
$file = fopen("test.txt","w");
echo fwrite($file,$img);
fclose($file);
但是没有任何内容被插入到test.txt
文件中,而且它总是空的。
有人可以就此问题提出建议吗?
编辑:
这是我的隐藏输入字段(#img):
<input type="hidden" id="img" value="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCABGAPoDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQ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NOdzuVxz6k0UUiiR9MmHO+P8z/hUf2F06vz7OaKKYihq+g2erW5ivYtxwQkgb5kJ7g/48cdK5KT4YQmRimqSqmTgGIEge5yM0UVSJZ//2Q==">
答案 0 :(得分:1)
您需要在ajax成功函数中添加window.clode()。 另外,假设您使用的版本晚于1.8
,我会稍微更改您的ajax调用 Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.
if(window.opener && !window.opener.closed) {
var img = window.opener.$("#img").val();
var request = $.ajax({
url:"folder/page.php",
data:{img:img},
type:"POST",
beforeSend:function(){ console.log(img); }
});
request.done(function( result ) {
// your result manipulation function
....
window.close(); // close pop-up
});
request.fail(function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
window.close(); // close pop-up
});
}
答案 1 :(得分:0)
试试这个:
if(window.opener && !window.opener.closed) {
var img = $('#img').val();
alert(img);
$.ajax({
url:"folder/page.php",
data:$.param({img:img}),
type:"POST",
contentType:"application/x-www-form-urlencoded",
success:function(result)
{
window.close(); // Closing the popup
}
});
}
使用$ .param传递您的数据。