0

I want to post a form and save response html code in a variable using javascript.
here is my code:

<script type="text/javascript">
function submitform()
{
  document.forms["myform"].submit();
}
</script>

<form id="myform" action="url" method="post" target="iframe">
<input type="hidden" name="arg" value="value">
<a href="javascript: submitform()">Go</a>
</form>
<iframe name="iframe"></iframe>



if I run this code response will be displayed in iframe. but in some cases after loading iframe page will redirected to source code. I searched many places to stop that redirection to happen but didn't find any way!!.
I just found one way by adding this javascript code:

window.onbeforeunload = function(){
            return false;
        };

but this way is annoying because it shows a dialog box when redirection and I can't ask users to click on that every time!!
so I chose to find a way to don't load response html code in browser that makes redirection! and instead of that save response in a variable and remove scripts and then print it!!

there is any way to do that?

Hossein
  • 161
  • 1
  • 8
  • That's what [Ajax](http://en.wikipedia.org/wiki/Ajax) is for. Have a look at https://developer.mozilla.org/en/AJAX/Getting_Started. – Felix Kling Jun 09 '12 at 12:30

3 Answers3

1

You're coding around corners. Use jQuery and its .ajax method. Instead of an iframe, drop the returned markup into a div.

Jeff Watkins
  • 6,243
  • 13
  • 17
  • I tested it but it doesn't work!! ajax doesn't work for domains outside of domain! it only works for main domain addresses. – Hossein Jun 09 '12 at 18:12
  • @Hossein That's correct and it has to be that way. Cross-domain communication is another story. Use `$.post` or `$.ajax` properly to call the same domain and after you understand that start looking for solutions labeled cross-domain or cross-origin – naugtur Jun 09 '12 at 21:16
1

You should look at jQuery's AJAX function, it's very flexible and easy to use. Or, even easier, try jQuery Form Plugin, which basically automates everything form-related. All you have to do is point the form and tell what do you want to be done before and/or after the form is submitted.

Tomek Buszewski
  • 6,696
  • 8
  • 58
  • 103
  • I tested it but it doesn't work!! ajax doesn't work for domains outside of domain! it only works for main domain addresses. – Hossein Jun 09 '12 at 18:12
  • Calm down, we hear ya. AJAX doesn't work cross-domain, which is obvious. If you want to use it that way, read up on JSONP. David Walsh covers it pretty well on his [blog](http://davidwalsh.name/jsonp). – Tomek Buszewski Jun 10 '12 at 13:32
0

You could use javascript to make a post request, rather than use a form. In that case, you can easily capture the response, provided you are posting to the same domain. (Cross-domain posts do not provide return values for security reasons). See JavaScript post request like a form submit on how to post with javascript.

Community
  • 1
  • 1
Mark
  • 15,245
  • 6
  • 95
  • 113
  • if I use this method user will be redirected to wherever post url and I don't want that! I just want to use response and prevent user from seeing that – Hossein Jun 09 '12 at 15:20
  • You are basically trying to hack something or to implement an incorrect solution to a problem. If you want to post to a webserver you don't control, it's not going to work without some hacking. If you control the server, google `JSONP` or `origin access headers`. – naugtur Jun 09 '12 at 21:20