php - how get response cross domain ajax -


i have code on server 1

$("#send-mail").click(function () {      var name = $('input#name').val(); // value of 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 error state true     }      var emailcompare = /^([a-z0-9_.-]+)@([da-z.-]+).([a-z.]{2,6})$/; // syntax compare against input     var email = $('input#email').val().tolowercase(); // value of 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 error state true     }       var comment = $('textarea#comment').val(); // value of 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 error state true     }      if (error == false) {         var datastring = $('#contact-form').serialize(); // collect data 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 php file }); 

then have other code on server 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);  }  } 

and code of contact form on server 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 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="* 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="* 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 comment.</div>                                     </div>                                 </div>                                 <div class="control-group">                                     <div class="controls">                                         <button id="send-mail" class="message-btn">send message</button>                                     </div>                                 </div>                             </form> 

as seen send post datas other server,, if click send on contact form nothing happens...but email send ok... need response when click send... contact form fields cleared... , text appears ...message has been sent..

im complete newbee on codes .. welcome¡¡

i ran code through jsfiddle.

and got error: cross-origin request blocked: same origin policy disallows reading remote resource @ http://guara.webposicionamientoweb.com.mx/pluton/php/mail.php. (reason: cors header 'access-control-allow-origin' missing).

in server code, try adding:

header("access-control-allow-origin: *"); 

or replace * html's domain. should allow request pass through.


Comments