0

i tray to access to endpoint and get the token, but i have an error : Access to XMLHttpRequest at 'https://ptyi.test.com:8443/users/authenticate' from origin 'http:localhost:51780' 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. How can i solve it?, why via postman access ? thanks in advance !!

       $.ajax({
           type:"POST",
           url: "https://ptyi.test.com:8443/users/authenticate",
           data: { username :"test@sur.com" , password : "ff12dfb3L$"},
           crossDomain: true,
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           success: function (result) {
              alert(result)
           },
           error: function (xhr, ajaxOptions, thrownError) {
                      alert(xhr.status + " \n" + xhr.responseText, "\n" + thrownError);
           },
       });
    });
yizero
  • 11
  • 5

1 Answers1

0

for security reason you cannot invoke ajax request to endpoint which is running on different port. I dont know what is your server side technology but if you have access to source code of https://ptyi.test.com:8443/users/authenticate then you can add Access-Control-Allow-Origin", "*"

private void addCorsHeader(HttpServletResponse response){
        //TODO: externalize the Allow-Origin
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, HEAD");
        response.addHeader("Access-Control-Allow-Headers", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept");
        response.addHeader("Access-Control-Max-Age", "1728000");
    }
Jigar Gajjar
  • 183
  • 2
  • 12