0
var settings = {
 async: true,
 crossDomain: true,
 url: "http://10.250.252.1:43001/parameter-api/calculation/outlines/",
 method: "GET",
 headers: {
 "Authorization": "Bearer AB2E2DB9C0894F0CAAE8ABBCBDA5E981",
 "cache-control": "no-cache",
 "Postman-Token": "e8b2a812-41c1-4b78-8e5b-2bbec1e9013f" 
            }
        }

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

i generated that code from postman, and everything works create there.

but when i try in javascript i get errors(in the console):

OPTIONS http://10.250.252.1:43001/parameter-api/calculation/outlines/ 404 (Not Found) Access to XMLHttpRequest at 'http://10.250.252.1:43001/parameter-api/calculation/outlines/' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

but when i remove my header i get only this error:

GET http://10.250.252.1:43001/parameter-api/calculation/outlines/ 401 (Unauthorized)

i use a cors extension for google chrome btw.

basicly i want to get data from that server, the server uses OAuth2 which i have a token for.

can someoen help me out.

DES PRO
  • 75
  • 1
  • 8
  • The `http://10.250.252.1:43001/parameter-api/calculation/outlines/` endpoint needs to be configured to respond to unauthenticated OPTIONS requests with a 200 OK. See the explanation at https://stackoverflow.com/questions/45405983/http-status-code-401-even-though-i-m-sending-an-authorization-request-header/45406085#45406085 – sideshowbarker Nov 27 '18 at 03:28

1 Answers1

0

Add this to your headers settings:

Access-Control-Allow-Origin: *

Kordonme
  • 2,314
  • 3
  • 20
  • 32
Zineb
  • 26
  • 2
  • It's often not a good idea to blindly add a wildcard as origin. I suggest editing the answer to adding only required hostnames. – Kordonme Nov 26 '18 at 13:19
  • it doesn't work, when i do it without extension it gives me another error: Access to XMLHttpRequest at 'http://10.250.252.1:43001/parameter-api/calculation/outlines/' 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. when i add the "Access-Control-Allow-Origin" : "*" and enable the cors extension, still wouldn't work – DES PRO Nov 26 '18 at 13:32
  • I see, it's a real CORS problem. Check this http://restlet.com/company/blog/2016/09/27/how-to-fix-cors-problems/ – Zineb Nov 26 '18 at 15:03
  • I fixed it, had to disable web security and data user on google chrome for it to work, tough i had cors extension, the server api is also gonna get adjusted itself for prevent things like that happens. thanks for you time – DES PRO Nov 30 '18 at 13:23