0

I tried to post a form content to a remote hosted PHP page, this is my code:

$.ajax({
        type: "POST",
        url: "http://url/page.php",
        data: formstring, // formstring=form.serialze();
        dataType: "text",
        success: function (data) { alert(data);},
        error: function () {}
    });

I started my PHP file with:

<?php
header('Access-Control-Allow-Origin: *');

print("hi");
die();

I see in Firebug only OPTIONS request:

Response Headers:
Access-Control-Allow-Orig...    *
Connection  Keep-Alive
Content-Encoding    gzip
Content-Length  22
Content-Type    text/html
Date    Tue, 25 Feb 2014 11:14:22 GMT
Keep-Alive  timeout=15, max=99
Server  Apache
Vary    Accept-Encoding
X-Powered-By    PHP/5.3.3-7+squeeze18

Request Headers:
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-us,fr;q=0.7,en;q=0.3
Access-Control-Request-He...    x-requested-with
Access-Control-Request-Me...    POST
Cache-Control   no-cache
Connection  keep-alive
Host    www.domain.tld
Origin  http://212.xxx.252.xxx
Pragma  no-cache
User-Agent  Mozilla/5.0 (X11; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0
Hamza
  • 1,087
  • 4
  • 21
  • 47
  • I think that there are browser security restrictions that deny responses from other origins too. So JSONP would normally be the best approach to take. – Jonathon Feb 25 '14 at 11:25
  • http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain ? – l2aelba Feb 25 '14 at 11:36

1 Answers1

0

for that you should enable the cors in your ajax request

Example,

 $.ajax({
        url: "http://url/page.php",
        type: "POST",
        crossDomain: true,
        data: formstring,
        success: function (response) {

        },
        error: function (xhr, status) {
            alert("error");
        }
    });
Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53
  • same problem after passing the crossDomain parameter, the error alert show up – Hamza Feb 25 '14 at 11:30
  • I'm pretty sure the accepting side needs to have access control for outside domains as well, it can't be a one-side thing – casraf Feb 25 '14 at 12:22