0

Here is the situation. With the following code below, connect to the API using Ajax. However, I'm getting the following messages:

OPTIONS [URL] 404 (Not Found)

and getting the statusText: "error" when I try to connect to the API.

Here is the code that I'm currently using:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript">
        $.ajax({
            url: "URL That I'm connecting to,
            beforeSend: function(xhr) {
             xhr.setRequestHeader("Content-Type", "application/json");
             xhr.setRequestHeader("Accept", "application/json");
           },
          dataType: "json",
         data: {
            client_id: "",
            client_secret: "",
            grant_type: "client_credentials"
           },
         type: "POST",
           success: function(response) {
             token = response.access_token;
              expiresIn = response.expires_in;
            },
            error: function(errorThrown) {
    console.log(errorThrown);

            }
         });
 </script>

Any help would be appreciated.

Thank you, Kevin

  • Which OAuth provider? Follow their documentation. – John Hanley May 10 '19 at 02:44
  • My two cents your request is preceded by a preflight request sent by your browser (with the method "OPTIONS") and not handled by the API which return in this case an error. If it's well the case, you'll have to request with a back-end method (PHP Curl for example) to avoid it. Here is a SO topic that can give more details on preflight requests in CORS context: https://stackoverflow.com/questions/29954037/why-is-an-options-request-sent-and-can-i-disable-it – Neii May 10 '19 at 23:55

0 Answers0