0

I have an iframe

<iframe class="browser" scrolling="no" name="navigation"></iframe>

An when a specific event is fired a POST request is sent to a servlet

$.post("LoggerServlet", { sharedSessionId: $('#sharedSessionId').val(), level: $('#level').val(), goTo: this.href} );

Is it possible to target the POST response to the iframe?

P.S. I think is possible use a workaround, like create a hidden form, populate it dynamically and use submit() method, but I'd prefer a "jQuery-native" solution if is possible.

P.P.S.

I've also tried something like this

$.post("LoggerServlet", { 
sharedSessionId: $('#sharedSessionId').val(), level: $('#level').val(), goTo: this.href
}).success(function(data) { alert(data); });

and

$.post("LoggerServlet", { 
sharedSessionId: $('#sharedSessionId').val(), level: $('#level').val(), goTo: this.href}, 
function(data) { alert(data); });

and

$.post("LoggerServlet", { 
sharedSessionId: $('#sharedSessionId').val(), level: $('#level').val(), goTo: this.href
}).success(function(data) {
    var iframe = $(".browser")[0].contentWindow.document;
    iframe.open();
    iframe.empty(); //just to see if the callback is called
    iframe.close();
    });
});

and the result is the same: none :(

I have a doubt:

the response to post message (viewed by firebug) is an html page

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>[...]</body></html>

Are we sure that the data parameter in function(data) get this kind of response, and not only a Json-like response?

Some suggestion? Thanks to all.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
jenjis
  • 1,077
  • 4
  • 18
  • 30
  • Did you try setting the context of the post call to the iframe itself? Like `$('iframe.browser').post(/* etc. */)` – inhan Oct 08 '12 at 12:49
  • @inhan - That's not possible, is it? (http://api.jquery.com/post/) – FixMaker Oct 08 '12 at 12:50
  • I never used `$.post` even once in my life but I know it's applicable to `$.ajax`. – inhan Oct 08 '12 at 12:52
  • Are you sure that your post request is actually returning a success (HTTP code 200)? I can't see anything wrong with the way that you are calling `$.post`; see it working in this fiddle: http://jsfiddle.net/uxFHB/1/ – FixMaker Oct 08 '12 at 12:53
  • @inhan - You can't call `post(..)` (or `$(..).ajax` for that matter) on a jQuery collection, which is what `$('iframe.browser')` will return. – FixMaker Oct 08 '12 at 12:54
  • My bad. It's the **context** setting there. `$.ajax({/*etc.*/ context:$('iframe.browser')})` and in **success** function you can call the iframe like `$(this).get(0)`. – inhan Oct 08 '12 at 13:31

1 Answers1

0

You could use .load to open the target file in the iFrame whilst passing the variabes.

Note: Using .load on iFrames can cause the function to fire twice; fixed by calling the function from within the iFrame itself. - See jQuery iframe load() event?

Community
  • 1
  • 1
Nw167
  • 169
  • 5