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

Asked

Viewed 55,629 times

32

Good evening, I’m trying to access a server from my local machine and I’m getting this answer:

No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'http://localhost' 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;
});

Somebody give me a light!

  • 1

    I don’t know the context of your application I’ve been there and what saved me was this extension Chrome: [https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi/related]. Browser extensions have fewer restrictions on making requests. At the end of the day, to make delete I had to make my application an extension.

  • Davi Aragão, it worked, thank you very much, but I really wanted to understand why I could not access without this app and what this app does that can free access

2 answers

20


In your request from $.ajax, you can pass the 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;

However, before that, make sure the CORS is enabled also 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 directly to your return as in the code below:

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

:)

  • I tried with "crossDomain: true" but I couldn’t get it yet, I haven’t installed it, I passed the header by "php" and even then it’s not working, it has the same error :(

  • On your php server you added something like: header('Access-Control-Allow-Origin:*');? follow link http://leocaseiro.com.br/aceso-externo-php-ajax-crodomain/

  • Yes CORS header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: GET, POST, PUT');

  • 1

    @Brunogomes tries to set this command before ajax (jQuery.support.Cors = true;) he in theory does the same thing in what I’ve spent looking at the most in the code he does a different scheme .. while this I will take a look at the extension code that you said worked out above :))

  • worked kkkkk vlw!!!

  • I’m glad it worked :)) I’ll update the post ..

  • 1

    How do I do this without using jQuery?

  • @Allanandrade de a look here :) http://answall.com/a/3251/30045

  • where I put jQuery.support.Cors = true; ?

  • 1

    @Guilhermealves you must use in the entrypoint of your script before the ajax call you want :)

Show 5 more comments

0

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

Header set Access-Control-Allow-Origin "*"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.