I am working on example.com, when some one logged on my site I would also want to check his authentication on abc.com, abc.com accepts POST request to check authentication.
I was thinking to use an iframe which auto submit a request to abc.com (along with exaple.com ) and set a cookie in abc.com domain too.
Please suggest me if there is any better way to handle this?
Could some one reply with an example how to submit an iframe with post params?
Thanks in advance!
Not sure that iFrame is the best practice here, but you can render a form inside your iFrame and submit it onload.
Of course you will need to pass some data to the form, so you can pass it via the src url with query string params and grab it inside the iFrame.
For post request you will need to set the form (inside the iFrame) as method="post"
You can set a form's target attribute to submit to an iframe:
<form name="frm1" id="frm1" action="http://someurl.com" method="post" target="myiframe">
You may also consider just using an ajax call with jQuery instead and set the httpmethod to "post", which is what I would do.
Related
I'm attempting to set up a same-page form submission mechanism for a client website. Their forms submit to a handler page which is on a different domain, so I cannot submit via ajax. Previously I have had success cloning my form into an iframe and submitting that (I don't need to retain control of the iframe, and I don't need to verify receipt, just post the data) but that is now being blocked in Chrome.
It would be relatively trivial to add cross-domain headers if I had control of the servers involved, but I don't.
Is there any way to resolve this, or do I have to tell my client that the method I was using is no longer available and they have no options without making server-side changes?
There's no need to clone the form. Just set target="iframe_name" on the <form> element.
Just target the iframe.
You can change form's target and action dynamically, if for example you need to reuse it for some other submission.
<form target="IFRAME_NAME">...
I have a form in my html which has an action url to different domain. After submit, it redirects the browser. I want it to be submitted but not redirected to another page. I know i can submit it with Ajax but since the domain is different it gives CORS error. I cannot mirror request in my own php file because form submission is made by virtual credit card payment system and it doesn't allow you to mirror it.
So, is there any way to submit form but prevent redirect without using ajax. As i know, it's impossible to make a request to different domain with ajax.
Solution 1
AJAX is possible across domains. You need the destination domain to set the appropriate headers on the response.
Access-Control-Allow-Origin: yourdomain.com
Access-Control-Allow-Methods: POST, OPTIONS
Access-Control-Allow-Headers: [anything else you might send]
return false from your ajax call or call preventDefault() to prevent the browser from redirecting the page.
Solution 2
Submit to your own server side code and emulate the transaction. However, you mentioned that they don't allow you to mirror it and I don't have details to address this problem. You can submit to your own server either AJAX (without CORS issues and no headers necessary) or normal POST.
Solution 3
Submit it to their server but have their server redirect back to a page on your own site.
Usually there is a way to set this up through whatever API control panel they give you.
Once again, without specific details, I can't directly address the problem
Solution 4
Load up the data in an iframe and submit in the iframe. This may have issues depending on the value of X-Frame-Options or if they have some sort of CSRF token but you should be able to POST a form in the iframe without redirecting the main page. The iframe can be hidden as well and submitted via JS (use submit() method on form--ajax not required)
New
I would imagine you can do something with an iFrame.
So the logic would be:
Have an empty <div> with display:none;
Have a <form action='self.php'>
Submit and preventDefault()
Build a URL with a querystring
Preferably a totally different page newself.php?var1=something&var2=anotherthing
Append an <iframe> to the hidden <div> with the URL+querystring
$('div').append('<iframe src="newself.php?var1=something&var2=anotherthing"><iframe>");
Get stuff from URL and build replica form
Give newself.php some JS to automatically submit the form to the API URL upon document load
Clear the hidden <div> of it's contents to await a new submission
Original
I am leaving this here because someone upvoted while I edited lol
In order to submit to a different domain they would have to open up their server to accept cross-domain POSTs.
So here the logic that you should be looking into:
AJAX submit to your PHP file and do e.preventDefault()
Use PHP to cURL the POST vars to the other domain. SO cURL Questions
Wait for response from other domain
Send a "yay" or "nay" back to your AJAX call
If the main goal is to keep the visitor on your website and submit visitor input to a third party website, you could submit the form to a local php script that performs a cUrl to the third party website.
That way, the data is posted 'under water' without showing all post parameters to your visitors and you get to keep your visitor on your own website.
The only thing is that your payment provider will probably redirect you to different pages depending on the payment result (succes/failure/unreacheable).
I have a form that I would like to submit. Once done redirect the page to another. However currently it is not working for me.
<form name="myname" method="post" action="actionurl" onsubmit="gotonext()">
function gotonext(){
var portalpath = window.location.pathname;
var myredirect = portalpath +"?uP_fname=msu/survey&command=display&sid=162";
alert(myredirect);
window.location.href = myredirect;
}
So the alert displays the correct url that I am seeking to go to. And the form submits the data to the correct actionurl. What is not working is the redirect.
Any ideas on where I might be going wrong?
Well, it's a bit more complicated. The problem is when the form is submitted, the client basically already starts processing a new request by URL specified in the form's action attribute.
One usual workaround is to use AJAX to submit the form, then trigger the redirect in that AJAX request's callback. That's easier, but has obvious limitations: AJAX requests won't cross domains.
Another approach (that might be useful in your case) is the following:
create a hidden iframe on the page with the form.
set the form's target attribute to that iframe's name
set onload event handler on this iframe, and trigger redirect in this handler.
Here's proof of concept.
I have a use case where i have to post the form data on a particular link and then forward/redirect the request to another link.
For e.g. In my page i have 3 textfields a,b,c and a submit button. On click of Submit i will post the request to say "http://www.abc.com/example" and then redirect the request to "http://www.def.com".
So for the end user after submitting the request he/she will see "http://www.def.com" and will not come to know that what happened in between.
We do not care what the response is from server where request is posted.
Any help/directions in implementing this use case will be highly appreciated.
Thanks.
Regards,
Mayank
Just specify the submit URL in form's action.
<form action="http://www.abc.com/example" method="post">
and let the code behind that URL redirect the request to the desired URL after postprocessing the request; the following example assumes that it's using the Servlet API:
response.sendRedirect("http://www.def.com");
No need for JavaScript here which would not work anyway on JS-disabled clients.
It would be preferable for performance to submit the form to your original server and then redirect to the other URL.
If you can't do this, then you could submit the request via AJAX request and on completion (success or error) change the window.location to the other URL.
I have My request parameters in the URL of current page. When I click on submit in the same page, parameters lost got lost in the new request. How to retain the request parameters even after submitting the form?
You can read the query params from the url and add them as a hidden fields into the form you are submitting. This will send the query string params along with the form.
Send the request parameters as form's GET params
Have the server set the parameters in a cookie so it is available in every page.
Put it in browser's local storage.
If submitting the form via POST, you can include request parameters in the form's action attribute, eg
<form method="post" action="action?id=123&foo=bar">
<input type="text" name="baz">
<input type="submit">
</form>
What you are asking for is state management. It can be done in several ways
HttpSession
Cookies
Hidden fields
URL rewriting.
Hidden fields is an easy way to do it; although the HTML can get more lengthy. I personally prefer HttpSession.