1

I'm trying to login to moodle from an external webpage using a post form to moodle, I used the next ajax to send the inputs:

var frm = $('#loginForm');

frm.submit(function(e) {
    e.preventDefault();
    $.ajax({
        type: frm.attr('method'),
        url: frm.attr('action'),
        data: frm.serialize(),
        xhrFields:{
            withCredentials:true
        },
        async:true,
        beforeSend: function (xhr){
            xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
        },
        success: function (data) {
            console.log("Logged");
        },
        error: function (data) {
            console.log("NOT Logged");
        },
    });
});

Now into the moodle's login/index.php I insert the headers to make possible the CORS connection:

header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: http://fabianmurillo.000webhostapp.com");
header("Origin" : "http://fabianmurillo.000webhostapp.com");

When I run the code, the browser returns an error:

..preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin'.. 

enter image description here

Dunno why browser is blocking the connection for login to moodle.

Thanks for your help.

  • 1
    Access-Control-Allow-Origin is a response header. You should read this https://stackoverflow.com/questions/15381105/cors-what-is-the-motivation-behind-introducing-preflight-requests – John-Philip Jul 10 '17 at 16:32
  • Possible duplicate of [jQuery CORS Content-type OPTIONS](https://stackoverflow.com/questions/12320467/jquery-cors-content-type-options) – John-Philip Jul 10 '17 at 16:34
  • @Koresh What do you mean with Access-Control-Allow-Origin is a response header? So what header should I use? can you help me? – fabiangothman Jul 10 '17 at 19:29
  • I invite you to take a look to the question and more specifically the answer in https://stackoverflow.com/questions/12320467/jquery-cors-content-type-options It contains information about how CORS works and interresting links. This should greatly help you. – John-Philip Jul 10 '17 at 20:04

0 Answers0