0

I'm trying to send an ajax request to an API running on the localhost but I get an error

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at 
http://127.0.0.1:9100/api/banks. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

The HTML page runs at: http://localhost/jqmob/

Though the same code works fine on postman so how to solve this Issue?

var form = new FormData();
form.append("lang", "en");

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://127.0.0.1:9100/api/banks",
  "method": "POST",
  "headers": {
    "Cache-Control": "no-cache",
    "Postman-Token": "b246c9ad-ce2d-4e39-bbef-5df0af6476e0"
  },
  "processData": false,
  "contentType": false,
  "mimeType": "multipart/form-data",
  "data": form
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

In the HTML page on the localhost I use jQuery 1.11.3

In the API appkication in .htaccess

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>
PHP User
  • 2,350
  • 6
  • 46
  • 87
  • The error message explains why it isn't working. – Kevin B Sep 06 '18 at 19:17
  • I know it's cross domain that's why I'm looking for a solution to make the request work from any other domain as it works from postman – PHP User Sep 06 '18 at 19:18
  • Right. you need the server that you are requesting from to conform to the rules of CORS. It ***must*** respond with the header that the error message states is missing. – Kevin B Sep 06 '18 at 19:18
  • Postman does not execute ajax requests. It executes web requests. Ajax vs a normal web request are not the same thing. – Taplar Sep 06 '18 at 19:19
  • added header('Access-Control-Allow-Origin: *'); in laravel controller before calss definition and code that worked for me – PHP User Sep 07 '18 at 12:59

0 Answers0