如何得到响应跨域AJAX [英] how get response cross domain ajax

查看:129
本文介绍了如何得到响应跨域AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器1本code

i have this code on server 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
});

然后我会对服务器2本等code

then i have this 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);
 }
 }

这是接触形式的服务器1 code

and this is the code of the 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 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到其他服务器,如果我点击发送上的联系形式没有任何反应......但电子邮件是发送好的...所以我需要的反应,所以当我点击发送.. 。联系表格字段都会被清除......和显示的文本...消息已发送。

as you seen i send the post datas to other server,, if i click send on the contact form nothing happens...but the email is send ok... so i need the response so when i click send... the contact form fields are cleared... and the text appears ...message has been sent..

在此codeS ..所以一些帮助即时完成newbee是welcome¡¡

im complete newbee on this codes .. so some help are welcome¡¡

推荐答案

我通过的jsfiddle跑你code。

I ran your code through a jsfiddle.

和我得到这个错误:跨域请求阻止:同源策略不允许读取远程资源时的 http://guara.webposicionamientoweb.com.mx/pluton/php/mail.php 。 (原因:CORS头访问控制 - 允许 - 原产地失踪)

And I got this error: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://guara.webposicionamientoweb.com.mx/pluton/php/mail.php. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

在你的服务器code,试着加入:

In your server code, try adding:

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

或更换*和HTML的域。这应该允许请求通过。

or replace * with your HTML's domain. This should allow the request to pass through.

这篇关于如何得到响应跨域AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆