2

我的代码是server1而 PHP 代码是server2。两台服务器是分开的。这段代码有什么问题?

var hr = new XMLHttpRequest();
hr.open("POST", "http://www.3675design.com/Clients/hl/developers/cyber/itgetbetter/email/index.php", true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function () {
    if (hr.readyState == 4 && hr.status == 200) {
        var data = JSON.parse(hr.responseText);
        for (var obj in data) {
            document.getElementById("conformation_and_error_header").innerHTML = "Video";
            document.getElementById("conformation_and_error_body").innerHTML = "Share yor video";

            $(document).ready(function () {
                $(".inline_popup_display_2").colorbox({
                    inline: true,
                    width: "50%"
                });
            });
        }
    }
}
hr.send("name=" + name + "&mail=" + mail + "&youtube=" + youtube + "&comments=" + comments + "");
4

1 回答 1

0

在 PHP 代码中,您可以添加允许跨域访问的响应标头:

header("Access-Control-Allow-Origin:*");

在 JS 代码中,如果您想发布一个 json 字符串而不是 kv,您应该更改您的请求标头:

//hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.setRequestHeader("Content-type", "application/json");
于 2014-07-31T02:50:22.623 回答