0

I'm trying to post form and to receive server response in a target iframe using just plain HTML. What i have so far is:

<iframe name="iframe" id="iframe" onload="loaded()" style="display:none"></iframe> 

than i have:

<form id="form" action="/rpc/test" method="post" target="iframe">
    <!--....... form input ............-->
    <input type="submit" class="button" name="Test" id="Test" value="Test">
</form>

and finally in the script tag i have the function loaded which need to process the response:

<script language="javascript" type="text/javascript">
function loaded() {
    var ifr = document.getElementById('iframe');
    var dc = ifr.contentWindow || ifr.contentDocument;
    if (dc.document) {
        dc = dc.document;
    }
    var rsp = dc.documentElement.innerText || dc.documentElement.textContent;
    var obj = JSON.parse(rsp);
    if (obj && obj.success === true) {
        // DO very important things
        return;
    }
}
</script>

This works absolutely ok in every possible browser except IE9 (don't test it on lower IE versions though). In IE9 the response is a file which contains the correct server response. IE9 asks me "Do you want to open or save .....?"

My server is nodejs. I set response:

resposnse.writeHead(200, { 'Content-Type': 'application/json' });

I can track in the IE9 network monitor that Content-Type is 'application/json'

Any ideas?

isJustMe
  • 5,452
  • 2
  • 31
  • 47
user732456
  • 2,638
  • 2
  • 35
  • 49
  • It may work on Chrome/Firefox, because those often have intelligent type recognition. Try to cURL or WGET the desired URL an print the headers the server actuallay sent back. Are you using node.js as server or did you have something like nginx in front? – Julius F Mar 13 '12 at 16:18

3 Answers3

0

In this case , you should take care the chrome or firefox made different post request from ie9 or 8

chrome will send OPTION request first test connection then send post request, but IE didn't so take care of this and you should try more setting on response like below

response.header("Content-Type", "application/json;charset=utf-8");
response.header("Access-Control-Allow-Methods","POST,GET,OPTIONS");
response.send("try");

and take care you should response chrome first

if(request.method==='OPTIONS')

 response.send(200);

hope this info help you

bigestgun
  • 1
  • 3
0

It seems to be a bad file mime type concern. Check that the server actually returns application/json output

Here are some similar posts :

Community
  • 1
  • 1
Jerome Cance
  • 8,103
  • 12
  • 53
  • 106
  • the Content-Type of the server response header is 'application/json'. It's processed just fine with Chrome and FF – user732456 Mar 13 '12 at 13:56
  • it seems this is a similar post to http://stackoverflow.com/questions/5388893/ie9-json-data-do-you-want-to-open-or-save-this-file are you sure you correctly set the content type ? – Jerome Cance Mar 13 '12 at 14:12
  • my server is nodejs server and correctly sets res.writeHead(200, { 'Content-Type': 'application/json' }); i tried to change to res.writeHead(200, { 'Content-Type': 'application/json; charset=UTF-8' }); In ie9 network console i see that Content-type of the respense is 'application/json' and correctly changes to 'application/json; chardset=utf-8 – user732456 Mar 13 '12 at 14:47
0

On the server side when you are returning make sure that the content type is set right to JSON.

In .NET, it will something like,

result.ContentEncoding = System.Text.Encoding.UTF8; 
result.ContentType = "application/json; charset=UTF-8
Teja Kantamneni
  • 17,402
  • 12
  • 56
  • 86
  • I commented the other answer as well but the content-type is indeeed 'application/json'. And as I said in the question it's processed by Chrome and FF correctly. – user732456 Mar 13 '12 at 13:58