CORS - No Access-Control-Allow-Origin header is present on the requested resource

26

Good evening, I'm trying to make an access from my local machine to a server and I'm getting this response:

  

No Access-Control-Allow-Origin header is present on the requested   resource. Origin ' link ' is therefore not allowed access.

But using Postman I can - these are the server requirements:

// Estes cabecalhos sao necessarios no CORS
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PUT');

My code:

$('form').submit(function(e){
    e.preventDefault();
    var submit = true;

    if( submit )
    var form = $(this).serialize();

    $.ajax({
        url: url,
        data: form,
        dataType: 'json',
        success: function(data) {
            console.log(data);
        },
        type: 'POST'
    });

    return false;
});

Someone gives me a light!

    
asked by anonymous 11.09.2015 / 03:09

2 answers

17

At your request of $.ajax , you can pass crossDomain: true as below:

 $.ajax({
    url: url,
    crossDomain: true,
    data: form,
    dataType: 'json',
    success: function(data) {
        console.log(data);
    },
    type: 'POST'
});

or

jQuery.support.cors = true;

But before that, make sure that CORS is also enabled on your server. For everything to work, CORS must be enabled on both client and server.

If you are using Node.js, you can add direct on your return as in the code below:

res.setHeader('Access-Control-Allow-Origin', '*');

:)

    
11.09.2015 / 03:42
0

The problem should be permission to use CORS in your apache, try putting this in the .htaccess file:

Header set Access-Control-Allow-Origin "*"
    
11.09.2015 / 18:52