X-Domain Post using XMLHttpRequest


X-Domain Post using XMLHttpRequest

我有以下代码块:

console.log(1);
            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'http://anothersite.com/deal-with-data.php');
            xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
            console.log(2);
            xhr.onreadystatechange = function () {
                console.log(3);
                if (this.status == 200 && this.readyState == 4) {
                       console.log(4);
                       xhr.send("formType="+thisFormType+
                        "&mediaCode="+3478+
                        "&car="+carID+
                        "&title="+$('#title').val()+
                        "&firstname="+$('#firstname').val()+
                        "&surname="+$('#lastname').val()+
                        "&tel="+$('#telephone').val()+
                        "&email="+$('#emailaddress').val()+
                        "&postcode="+$('#postcode').val()+
                        "&add1="+$('#add1').val()+
                        "&add2="+$('#add2').val()+
                        "&add3="+$('#add3').val()+
                        "&town="+""+
                        "&county="+""+
                        "&optin-post="+$('#optin-post').attr('checked')+
                        "&optin-tel="+$('#optin-tel').attr('checked')+
                        "&optin-email="+$('#optin-email').attr('checked')+
                        "&optin-sms="+$('#optin-sms').attr('checked')+
                        "&tarID="+targetID+
                        "&campID="+campaignID+
                        "&subID="+subjectID
                    );
                    console.log(5);
                }
            }
            console.log(6);

所以,除了xhr.onreadystatechange之外,一切都会触发 - 我从来没有得到console.log(4) - 我已经在PHP和htaccess中启用了Access-Control-Allow-Origin,并尝试了大量的Javascript后期函数。

问题是,作为一项要求,我需要将数据从不支持服务器端语言的服务器上的表单发布到另一个域以处理数据。

有什么想法吗?快把我逼疯了!

编辑:我也用$.ajax尝试过

$.ajax({
            url: 'http://anothersite.com/deal-with-data.php',
            type: "post",
            crossDomain: true,
            data: {
                "formType":thisFormType,
                "mediaCode":3478,
                "car":$('#car').val(),
                "title":$('#title').val(),
                "firstname":$('#firstname').val(),
                "surname":$('#surname').val(),
                "tel":$('#telephone').val(),
                "email":$('#email').val(),
                "postcode":$('#postcode').val(),
                "add1":$('#add1').val(),
                "add2":$('#add2').val(),
                "add3":$('#add3').val(),
                "town":"",
                "county":"",
                "optin-post":$('#opt-post').attr('checked'),
                "optin-tel":$('#opt-tel').attr('checked'),
                "optin-email":$('#opt-email').attr('checked'),
                "optin-sms":$('#opt-sms').attr('checked'),
                "tarID":targetID,
                "campID":campaignID,
                "subID":subjectID
            },
            beforeSend: function() {
                $('#submit').val('Sending...').attr('disabled','disabled');
            },
            success: function(data) {
                console.log("success call");
                console.log(data);
            },
            error: function(err) {
                console.log("error call");
                console.log(err);
            }
        });

现在我也尝试在httpd.conf中启用它:https://serverfault.com/a/378776/36601

你不应该在 xhr.onreadystatechange 中传递xhr.send。执行以下操作:

var xhr = new XMLHttpRequest();
xhr.open('POST', 'myurl', true);
xhr.onload = function () {
    if (xhr.status === 200 && xhr.readyState === 4) {
        // do some cool stuff
        console.log('You got a successfull request');
    }
};
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.send(); // pass your params here

您可能还需要设置 Access-Control-Allow-Methods: POST 标头。阅读有关 https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS 的更多信息