2

I have a webpage divided on subdomains by an .HTACCESS simple rule. Subdomains contains an user's profile, and it has a comment form, which points to the main domain to make the POST request:

function comment(){
if($('#comment').val() && $('#comment-author').val() && $('#comment-mail').val() && $('#comment-tc').is(':checked')){
$.post('http://www.example.com/user-profile/ajax.php', {'fnc':'make-comment', 'text': $('#comment').val(), 'mail': $('#comment-mail').val(), 'name': $('#comment-author').val()},
function(d){$('#comment-area').html('Your comment was sent! Thanks '+$('#comment-author').val()+'!');});
}
} 

It works right if I do this on the main domain, but if I try it from a subdomain, for example ryanc.domain.com, Firebug throws an 200 OK, but at the same time an error:

The Error

It doesn't have response nor effect, and I need this to work. I don't know why this is not working, any clue?

Ryan Casas
  • 666
  • 5
  • 19
  • Not an exact duplicate, but there is some helpful information here: [jquery solutions to post to another site from static html page](http://stackoverflow.com/questions/2102268/jquery-solutions-to-post-to-another-site-from-static-html-page) –  Jan 02 '12 at 17:41
  • Isn't another site, it's on the same domain, on the same server. Thanks anyway :3 – Ryan Casas Jan 02 '12 at 17:45
  • server1.example.com and server2.example.com are considered as cross domain for AJAX requests, so it still applies. –  Jan 02 '12 at 17:46
  • Is there some way to make it work? – Ryan Casas Jan 02 '12 at 17:51
  • 1
    Here's a closer SO duplicate that should help you: [How do I send a cross-domain POST request via JavaScript?](http://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript) –  Jan 02 '12 at 17:54

1 Answers1

4

According to the same origin policy restriction you need to use the exact same host and subdomain.

For example if you try to send an AJAX request to from http://ryanc.domain.com to http://www.domain.com you violate this policy and the browser restricts the request.

There are a couple of workarounds. Here's a good article that's worth checking and which shows different workarounds.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • What can I do for make it works? Is there some alternative? Thanks for the answer, anyway :D – Ryan Casas Jan 02 '12 at 17:44
  • @RyanCasas, yes, there are workarounds. I have updated my answer to post a link to an article which illustrates the different alternatives you have. They include using a server side script bridge, Flash Proxy, JSONP, YQL, ... – Darin Dimitrov Jan 02 '12 at 17:45
  • Hm, interesting information, but I'm not trying to get information from the server, I'm trying to make a POST request to send information to that file. Sorry if I'm idiot but I'm a bit ofuscated of searching solution for this. – Ryan Casas Jan 02 '12 at 17:50
  • @RyanCasas, you can't directly send such request because, as I already stated in my answer, you are violating the same origin policy that is built-in browsers. That simply is not allowed for security reasons. Sorry. You will have to use one of the workarounds if you want to be able to perform such request. – Darin Dimitrov Jan 02 '12 at 17:51