0

I want to login in my Site Drupal 8.2.3 from another application using Ajax, but is returning error all the time.

$.ajax({
    url : "http://localhost/Drupal-tuto-test/user/login",
    type : 'POST',
    data: {
        'username' : 'admin',
        'password' : 'admin'
    },
    dataType : 'json',
    error : function(data) {
        console.log('Error ',data);
    },
    success : function(data) {
        console.log('Success ',data);
    }
});
0

2 Answers 2

3

Your POST data isn't correct, you should be using "user" and "pass". For a a more complete response, see my answer here.

0
1

If you're making an API request from a different origin in JS/AJAX, you will need to configure cross-origin-resource-sharing (CORS)

This page has more details on D8 CORS opt-in:

This is particularly helpful for fully decoupled Drupal sites which have JS that needs to talk to a Drupal 8 site's REST API. In such cases, that Drupal 8 instance often runs on a separate domain. Due to the same origin policy those requests will be blocked by the browser.

https://www.drupal.org/node/2715637

Note: Make sure your allowed-origin is specific to the domain you want to permit access from. Using "*" will leave it wide-open to the entire internet. Better to be specific.

Not the answer you're looking for? Browse other questions tagged or ask your own question.