3

I wanna create Ajax request from empty HTML with jQuery. I do like this

$.ajax({
    url: "http://****.com/rest/V1/integration/admin/token?username=***&password=****",
    type: "POST",
    success: function (d) {
      alert(d);
    }
  });

but always I get error

Access to XMLHttpRequest at 'http://localhost:8000/rest/V1/integration/admin/token?username=magento&password=magento2' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 

I know I should add some header before send request but jquery does not support headers. Anyone gets this error?

0

You can try this and check, use method instead of type:

$.ajax({
    url: "http://****.com/rest/V1/integration/admin/token?username=***&password=****",
    method: "POST",
    success: function (d) {
      alert(d);
    }
});  

Or with headers using jquery and prototype

require([
    'jquery',
    'prototype'
], function($){
    $.ajax({
    url: "http://****.com/rest/V1/integration/admin/token",
    method: "POST",
    "headers": {
        "username": "magento",
        "password": "magento2"
    },
    success: function (d) {
    alert(d);
    }
  });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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