Search code examples
javascriptjqueryjsonajaxget

API call over AJAX


I'm new to AJAX and I want to generate a password over this API. I want to use AJAX but dont know exactly how and where to specify the parameters.

$.ajax({
    url: 'https://passwordwolf.com/?length=10&upper=off&lower=off&special=off&exclude=012345&repeat=5',
    dataType: "text json",
    type: "POST",
    data: {upper: off, lower: on, special: off},
    success: function(jsonObject,status) {

        console.log("function() ajaxPost : " + status);

    }
});

Many Thanks, Pls don't hate my programming skills!


Solution

  • Having a look at their API:

    • You should use the GET method, not POST.
    • The url should be https://passwordwolf.com/api/ (notice the /api at the end).
    • Besides, passwordwolf doesn't accept CORS, so you should probably call that service from your server side and mirror it to your frontend with the appropriate CORS headers.

    See demo below (it uses cors-anywhere to circunvent the CORS problem).

    It also shows how to correctly use an object to set params.

    var CORS = 'https://cors-anywhere.herokuapp.com/';
    
    $.ajax({
      url: CORS + 'https://passwordwolf.com/api/',
      dataType: "json",
      type: "GET",
      data: {
        length: 10,
        upper: "off",
        lower: "off",
        special: "off",
        exclude: "012345",
        repeat: 5
      },
      success: function(jsonObject, status) {
        console.log("ajax result: " + JSON.stringify(jsonObject));
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>