2015-09-25 100 views
0

我已經在服務器這個代碼1如何獲得響應跨域AJAX

$("#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 
}); 

然後我對服務器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"; //"[email protected]"; 

//from email adress 
$emailFrom ="[email protected]"; //"[email protected]"; 

//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-][email protected]([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 <[email protected]>' . PHP_EOL . 
$headers .= "Reply-to: <$email>". 
    'X-Mailer: PHP/' . phpversion(); 


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

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

這個其他代碼,這是接觸形式在服務器上的代碼1

<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> 

,你看到我發的帖子DATAS到其他服務器,,如果我點擊發送的聯繫人形式沒有任何反應上......但電子郵件是發送確定...所以我需要的反應,所以當我點擊發送...聯繫表格字段都會被清除......而出現的文字...郵件已發送..

IM完整newbee在這個代碼..所以一些幫助是值得歡迎的

+0

向服務器1發出請求,並讓服務器1使用cURL向服務器2發送數據 – charlietfl

+0

不知道該怎麼做,,,即時閱讀一些關於... tnks –

回答

0

我通過jsfiddle運行你的代碼。

而且我得到了這個錯誤:跨來源請求阻止:同源策略不允許在http://guara.webposicionamientoweb.com.mx/pluton/php/mail.php讀取遠程資源。 (原因:缺少CORS頭'Access-Control-Allow-Origin')。

在你的服務器代碼,嘗試添加:

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

或更換*您HTML的域。這應該允許請求通過。

+0

服務器其付費主機,,,我不能改變它... –

+0

我沒有免費託管有關設置標題的問題。我不確定付費託管會如何撤銷權利。您是否嘗試輸入此代碼? – user2676208