代码之家  ›  专栏  ›  技术社区  ›  Gucci

AJAX上的API调用

  •  0
  • Gucci  · 技术社区  · 7 年前

    我是AJAX新手,我想在 this API。我想使用AJAX,但不知道如何以及在哪里指定参数。

    $.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);
    
        }
    });
    

    非常感谢,请不要讨厌我的编程技巧!

    1 回复  |  直到 7 年前
        1
  •  3
  •   acdcjunior Mukul Kumar    7 年前

    看看他们的API:

    • 您应该使用 GET 方法,而不是 POST
    • url应为 https://passwordwolf.com/api/ (注意 /api 最后)。
    • 此外,passwordwolf不接受CORS,所以您可能应该从服务器端调用该服务,并使用 appropriate CORS headers

    请参阅下面的演示(它使用cors anywhere来解决cors问题)。

    它还显示了如何正确 使用对象设置参数

    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>