rookie Bounty
rookie Bounty

Reputation: 127

Javascript catch server response in iframe

I have written a javascript to post data to a server. This seems to be working but I'm having trouble catching the servers response in iframe. I want to catch the servers response in iframe or in a datastream without forwarding to a other page or refreshing but i will settle with just stopping the page from being forwarded or refreshed.

I have tried adding target="my_frame" to the button and the form attributes but it doesn't seem to work.

My code, I slightly modified my code for presentation:

 <html>
  <head>

    <title>post</title>

  </head>
  <body>

<script language="javascript">

   var myDictionary = [];
    myDictionary["username"] = "stackoverflow";
    myDictionary["password"] = "mypassword";


function post(dictionary, url, method) {
    method = method || "post"; 

    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", url);

    for (key in dictionary) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden"); 
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", dictionary[key]);
        form.appendChild(hiddenField); 
    }

    document.body.appendChild(form); 
    form.submit();
}
</script>

<input type="button" target="my_frame" value="Submit" onclick="javascript:post(myDictionary, 'http://www.website.com/register/registernow');" />

    <iframe name="my_frame" src="">

  </body>
</html>

Thanks for reading and for all replies.

Upvotes: 2

Views: 1795

Answers (1)

Iftah
Iftah

Reputation: 9572

What you are looking for is an Ajax POST.

You don't need to create a form and submit it, instead you need XMLHttpRequest, or if you are using jquery you can use $.post or $.ajax.

See for example http://blog.mgechev.com/2011/07/21/ajax-jquery-beginners/

Upvotes: 1

Related Questions