我有以下提交到iframe的jquery函数。从PHP发回的消息是json。我无法弄清楚如何在jquery中收到此消息,以便我可以将其显示给用户。
function IframeSubmit(){
// remove iframe if exists
$('#hiddenIframe').remove();
// change target attribute on form
form.attr('target', 'hiddenIframe');
// create and add iframe to page
$('<iframe />', {
name: 'hiddenIframe',
id: 'hiddenIframe',
style: 'display:none'
}).appendTo('body');
// on response from php file
$('#hiddenIframe').load(function(){
// process received message here ...
});
}
谢谢!
答案 0 :(得分:0)
我有类似的代码:
$(document).ready(function(){
$('form#myform').submit(function(){
$("#hiddenIframe").remove();
$('<iframe name="hiddenIframe" />').appendTo('body').attr({'id': 'hiddenIframe'});
var Frame = $('#hiddenIframe');
var newSrc = 'about:blank?nocache=' + Math.random(); //force new URL
Frame.attr('src', newSrc);
var iframe = $('#hiddenIframe').load(function(){
var response = iframe.contents().find('body').html();
var txt = $.parseJSON(response);
$('#message').append(txt.message);
if(txt.error == false){
$.ajax({
type: 'POST',
url: '?myurl',
dataType: 'json',
data: {
//some data
},
success: function(data){
//some action
}
});
}
});
});
});
iframe中的输出是:
var response = iframe.contents().find('body').html();
var txt = $.parseJSON(response);
读取响应如:txt.error或txt.message
PHP部分:
$this->message['error'] = true;
$this->message['message'] = "Problem!";
echo/print json_encode($this->message);