If I use an ajax request to post a form without actually creating any form or input elements on the page, is a captcha still needed to block spambots?
Are there any spambots out there clever enough to run JavaScripts and submit a ajax post?
Related
I am using html5, javascript and JSP for my project. I want to know if there is some method that i can used to execute a query from my servlet without actually posting back the page. i know it can be done in ASP.net but i do n't how it can be be done in java script and JSP. Actually i have a dynamic webpage displaying data from server.what i want is that in a click event of button i want to execute a query form server and update it on the page. i know i can submit the form but it will submit the page which i want to avoid.Any suggestion......
regards
nquazi
You can use an AJAX request to submit inputs and get back an output without reloading that page. Here is a previous stackoverflow answer that shows you how to do a HTTP GET request.
HTTP GET request in JavaScript?
You will then need to process your inputs, run the query, and send back an output on the backend server.
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 want to intercept my form's submit response via javascript. Submit is normally done by a submit button or jquery's .submit() (with no params). Is there a way to intercept that submit response like;
.submit().done(callback)
I know .submit() does not have support for this, however creating a post request like finding every input in that form and getting their values into data object which we will then pass to .post() method seems a bit clumsy to me. Besides there will be no advantage of using <form> tags in html in that approach.
Any suggestions for this?
No, you can't.
Besides, when you do .submit() or click submit button, the page unloads and a new page loads. which means all java script on the page is gone so there is no point in having a callback.
I have a submit input in a form with an onclick method. The onclick method correctly adjusts the action of the form and allows the form to submit (submission is handled naturally, not through a javascript submit). What I need to do is add a http request header (X-Requested-With = XMLHttpRequest to be exact). Is there a way to ensure the form post is sent with that header? The post cannot be submitted via javascript using the form.submit() method.
According to this answer what you're asking for is impossible in its current form. However you could modify where the form submits too. For example POST to:
www.mysite.com/XMLHttpRequest or www.mysite.com/NormalRequest
so that the server understands the context of the request.
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.