How to post a form across subdomains
I need to pass a set of values in a form such as name, lastname, age, etc. to a PHP file in a different subdomain.
For example, form.html is located at http://subdomain1.website.com/form.html
, when I press the submit button it passes the data in the form to http://subdomain2.website.com/doform.php
and inserts it into the database.
How do I pass this data?
I've tried:
$.post('http://subdomain2.website.com/doform.php', {key : 'fdsjfojdsfmkldskfoidsjk'}, function(data){
alert(data);
});
It fails with permission denied
. Can I fix this?
JSONP doesn't allow POST operation. If the subdomain2 accepts data in POST only then you should consider using a server-side proxy file. Send your POST data via ajax to a php file at subdomain1 and this script will finally send a POST request to subdomain2.
A Google search for "javascript cross-domain post" got me this:
How do I send a cross-domain POST request via JavaScript?
Consider using JSONP. It's the only real cross-browser solution for cross-domain requests. Unfortunately, there is no way to perform a JSONP request as a POST operation.
There's also a decent article by Use JQuery that you might find helpful:
http://usejquery.com/posts/9/the-jquery-cross-domain-ajax-guide
精彩评论