Send form without reloading the page being from different domains

0

I need to make a method that does not reload the page and send the data to a server in another domain. But I'm having problems with XMLHttpRequest . Does anyone know any method to do this and can send the data to the script PHP that is on the other server outside the domain?

This is the method I was trying to do:

$(function() {

    $('#form1').submit(function(event){
        event.preventDefault();
        var nome = $("#Nome").val();
        var email = $("#Email").val();

        if (nome!='' & email!='') {
            $.ajax({
                type: "POST",
                url: "Insert_blog_tricae2.php",
                contentType: "application/json;         charset=utf-8",
                crossDomain  : "true",
                dataType: "jsonp",
                asynch: true,
                data: {"Nome": nome, "Email":email},
                success: function(retorno){
                    $('#resultado').html(nome+"<br>"+email).fadeIn();
                    $('#Nome').val('');
                    $('#Email').val('');
                    $('#resultado').fadeOut(10000);
                }
            });
        } else {
            $('#resultado').html('<center>Existem campos incompletos no formul&aacute;rio.<br> Favor preencher todos.</center>').show();
        }
        return false;
    })
})
    
asked by anonymous 13.07.2015 / 18:08

1 answer

1

The problem must be on the server that is receiving the post.

You should use Access-Control-Allow-Origin on the server that will receive the data.

header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');

Instead of $_SERVER['HTTP_ORIGIN'] you can set the server to prevent other domains from communicating with your server that is receiving your request.

You can learn more about Access Control CORS (in English) on the site MDN and html5rocks (also in English)

    
14.07.2015 / 05:20