Php 如何获得跨域ajax的响应

Php 如何获得跨域ajax的响应,php,ajax,dns,response,Php,Ajax,Dns,Response,我在服务器1上有此代码 $("#send-mail").click(function () { var name = $('input#name').val(); // get the value of the input field var error = false; if (name == "" || name == " ") { $('#err-name').show(500); $('#err-name').delay(4000

我在服务器1上有此代码

$("#send-mail").click(function () {

    var name = $('input#name').val(); // get the value of the input field
    var error = false;
    if (name == "" || name == " ") {
        $('#err-name').show(500);
        $('#err-name').delay(4000);
        $('#err-name').animate({
            height: 'toggle'
        }, 500, function () {
            // Animation complete.
        });
        error = true; // change the error state to true
    }

    var emailCompare = /^([a-z0-9_.-]+)@([da-z.-]+).([a-z.]{2,6})$/; // Syntax to compare against input
    var email = $('input#email').val().toLowerCase(); // get the value of the input field
    if (email == "" || email == " " || !emailCompare.test(email)) {
        $('#err-email').show(500);
        $('#err-email').delay(4000);
        $('#err-email').animate({
            height: 'toggle'
        }, 500, function () {
            // Animation complete.
        });
        error = true; // change the error state to true
    }


    var comment = $('textarea#comment').val(); // get the value of the input field
    if (comment == "" || comment == " ") {
        $('#err-comment').show(500);
        $('#err-comment').delay(4000);
        $('#err-comment').animate({
            height: 'toggle'
        }, 500, function () {
            // Animation complete.
        });
        error = true; // change the error state to true
    }

    if (error == false) {
        var dataString = $('#contact-form').serialize(); // Collect data from form
        $.ajax({
            url: $('#contact-form').attr('action'),
            type: "POST",
            data: dataString,
            timeout: 6000,
            error: function (request, error) {

            },
            success: function (response) {
                response = $.parseJSON(response);
                if (response.success) {
                    $('#successSend').show();
                    $("#name").val('');
                    $("#email").val('');
                    $("#comment").val('');
                } else {
                    $('#errorSend').show();
                }
            }
        });
        return false;
    }

    return false; // stops user browser being directed to the php file
});
<div id="successSend" class="alert alert-success invisible">
                                <strong>Well done!</strong>Your message has been sent.</div>
                            <div id="errorSend" class="alert alert-error invisible">There was an error.</div>
        <form id="contact-form"  method="post" action="http://guara.webposicionamientoweb.com.mx/pluton/php/mail.php">
                                <div class="control-group">
                                    <div class="controls">
                                        <input class="span12" type="text" id="name" name="name" placeholder="* Your name..." />
                                        <div class="error left-align" id="err-name">Please enter name.</div>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <div class="controls">
                                        <input class="span12" type="email" name="email" id="email" placeholder="* Your email..." />
                                        <div class="error left-align" id="err-email">Please enter valid email adress.</div>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <div class="controls">
                                        <textarea class="span12" name="comment" id="comment" placeholder="* Comments..."></textarea>
                                        <div class="error left-align" id="err-comment">Please enter your comment.</div>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <div class="controls">
                                        <button id="send-mail" class="message-btn">Send message</button>
                                    </div>
                                </div>
                            </form>
然后我在服务器2上有另一个代码

<?php

include 'functions.php';

if (!empty($_POST)){

$data['success'] = true;
$_POST  = multiDimensionalArrayMap('cleanEvilTags', $_POST);
$_POST  = multiDimensionalArrayMap('cleanData', $_POST);

//your email adress 
$emailTo ="****@hotmail.com"; //"yourmail@yoursite.com";

//from email adress
$emailFrom ="contact@yoursite.com"; //"contact@yoursite.com";

//email subject
$emailSubject = "contacto teklife";

$name = $_POST["name"];
$email = $_POST["email"];
$comment = $_POST["comment"];
if($name == "")
$data['success'] = false;

if (!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email)) 
$data['success'] = false;


if($comment == "")
 $data['success'] = false;

 if($data['success'] == true){

 $message = "NAME: $name<br>
 EMAIL: $email<br>
 COMMENT: $comment";


 $headers = "MIME-Version: 1.0" . "\r\n"; 
 $headers .= "Content-type:text/html; charset=utf-8" . "\r\n";
 $headers = 'From: TEKLIFE <jsparrow@blackpearl.com>' . PHP_EOL .
 $headers .= "Reply-to: <$email>".
    'X-Mailer: PHP/' . phpversion();


 mail($emailTo, $emailSubject, $message, $headers);

 $data['success'] = true;
 echo json_encode($data);
 }
 }

我通过JSFIDLE运行了您的代码

我得到了这个错误:跨源请求被阻止:同源策略不允许读取远程资源。(原因:缺少CORS标头“访问控制允许原点”)

在服务器代码中,尝试添加:

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

或者将*替换为HTML的域。这应该允许请求通过。

向服务器1发出请求,并让服务器1使用Curl将数据发送到服务器2。不知道如何做,,,我正在阅读一些关于…tnksI没有关于设置头的免费托管问题。我不确定付费主机将如何撤销这项权利。你试过输入这个代码吗?