问题
I'm trying to post data with jQuery in an asp.net application to a popup.
If the popup opens, I'm getting three errors. The first error is:
Errror: the value of the property is null or undefined not a function object
(error code [code is in popup site]:http://www.suckmypic.net/26449/e65f2d77.png, orig. code [code is in popup site]:http://www.suckmypic.net/26450/7dfdf013.png)
then I'm getting two errors
of private functions which are included correctly.
Then - if I'm reloading the popup window, everything is working fine.
I open the popup in this way:
$.post('popup.aspx', { X: $("#X1").val(), XX: varX, XXX: varXY, Z: varZ}, function (result) {
hWndHelp = window.open('', 'help', cStyle);
hWndHelp.focus();
hWndHelp.document.open();
hWndHelp.document.write(result);
hWndHelp.document.close();
});
(it's stored in a function that I'm calling on pressing the f1 key which is working fine)
I'm referencing in the mainpage and in the popup window all my functions and the jquery library.
Edit
The code for the cStyle
var:
var WIN_STYLE_RESIZE =
'resizable = yes, ' +
'status = yes, ' +
'scrollbars = yes';
var cStyle =
WIN_STYLE_RESIZE + ', ' +
'width = ' + w + ', ' +
'height = ' + h + ', ' +
'top = ' + y + ', ' +
'left = ' + x;
(w, h, y, x are calculated numbers, basing on window size)
If I change it simply to 'width=600,height=400'
, the error still occurs.
If I send my variables via get
it also works, but i need to hide the variables in the URL.
Working get method:
var getUrl = "popup.aspx?X="+$('#X1').val()+"&....";
hWndHelp = window.open(getUrl, 'help', cStyle);
Another Edit: Just tried chrome and firefox - no error there. But I need the code to work with IE.
回答1:
Give some time to open the window
before accessing it. Try this.
$.post('popup.aspx', { X: $("#X1").val(), XX: varX, XXX: varXY, Z: varZ}, function (result) {
var hWndHelp = window.open('', 'help', cStyle);
setTimeout(function(){
hWndHelp.focus();
hWndHelp.document.open();
hWndHelp.document.write(result);
hWndHelp.document.close();
}, 400);
});
回答2:
First of all thanks for the replies.
I've tried every answer, but I'm still always get the errors in internet explorer.
I've found a workaround, but it does not make me happy because i think generating a new form with input fields is too much for my needs.
Since it's the only working option for posting my data into a popup without getting the jQuery error, I've decided to use it.
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "popup.aspx");
form.setAttribute("target", "help");
var input = document.createElement("input");
input.type = "hidden";
input.name = "X";
input.value = $("#X").val();
form.appendChild(input);
var input2 = document.createElement("input");
input2.type = "hidden";
input2.name = "XX";
input2.value = varX;
form.appendChild(input2);
var input3 = document.createElement("input");
input3.type = "hidden";
input3.name = "XXX";
input3.value = varXY;
form.appendChild(input3);
var input4 = document.createElement("input");
input4.type = "hidden";
input4.name = "Z";
input4.value = varZ;
form.appendChild(input4);
document.body.appendChild(form);
hWndHelp = window.open("about:blank", "help", cStyle);
hWndHelp.focus();
form.submit();
document.body.removeChild(form);
original source: http://taswar.zeytinsoft.com/2010/07/08/javascript-http-post-data-to-new-window-or-pop-up/
回答3:
I guess jquery library are getting loaded on both pages.
And also you have included $.post in $(function() { ....... }
$.post shorthand function for $.ajax , so it works as asynchronous . I suggest to make it synchronous if possible.
回答4:
try this:
$.ajax({
type: 'POST',
url: 'popup.aspx',
data: { X: $("#X1").val(), XX: varX, XXX: varXY, Z: varZ},
success: function(data, textStatus, jqXHR) {
var hWndHelp = window.open('about:blank', 'help', cStyle);
hWndHelp.focus();
hWndHelp.document.open();
hWndHelp.document.write(data);
hWndHelp.document.close();
},
dataType: 'html'
});
回答5:
I made a jQuery plugin to do this. Doesn't have to be jQuery, but mine is.
(function ($) {
$.submitFormToPopup = function (action, params, target, windowOpts) {
var formID = 'submitFormToPopup';
var form = $('<form />')
.attr('method', 'post')
.attr('action', action)
.attr('target', target)
.attr('id', formID);
$.each(params, function (key, value) {
form.append($('<input />')
.attr('type', 'hidden')
.attr('name', key)
.attr('value', value));
});
$(document.body).append(form);
window.open('about:blank', target, windowOpts);
form.submit();
$(document.body).remove('#' + formID);
};
})(jQuery);
回答6:
Looks like you are missing jQuery in popup.aspx. Make sure it´s included.
来源:https://stackoverflow.com/questions/9130753/asp-net-jquery-post-data-with-jquery-to-a-popup-ie