我是 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);
}
});
非常感谢,请不要讨厌我的编程技巧!
最佳答案
看看他们的 API:
- 您应该使用
GET
方法,而不是POST
。 - 网址应为
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>
关于javascript - 通过 AJAX 调用 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49132184/