gpt4 book ai didi

jquery - 如何在 jQuery 中发出 get 和 post API 请求?

转载 作者:行者123 更新时间:2023-12-01 06:44:04 26 4
gpt4 key购买 nike

我有一个用 Ruby 编写的 API 代码,它在循环中调用请求中的 API,每次我们最多获得 50 个结果。但我想在 jQuery 中发出这个 API 请求。我不知道该怎么做。

这是我的 Ruby 代码:

totalsequence = []
start = 0
begin
response = HTTParty.get("https://api.ontraport.com/1/objects?objectID=5&start=" + start.to_s,
{
:headers => { 'Api-Appid' => '', 'Api-Key' => ''}
})
totalsequence = totalsequence + response['data']
start += 50
end until (response['data']).size == 0
render json: JSON.pretty_generate(totalsequence)

所以我希望将这段代码转换为 jQuery。

最佳答案

在 jQuery 中,您可以使用 jQuery.ajax像这样:

$.ajax({
url: "https://api.ontraport.com/1/objects?objectID=5&start=" + start.to_s,
type: 'GET',
headers: {
'Api-Appid': '2_7861_X3YeBz0j1',
'Api-Key': 'NXhYJvz2AsywN80'
},
data {
'key1': 'value1',
'key2': 'value2',
...
},
dataType: 'json',
success: function(data) {
// handle success here via accessing data variable
// returned from the server as response
},
error: function(error) {
// handle error here via accessing error variable
},
});

data 属性指定要发送到服务器的数据,该数据应采用键值对形式。

查看更多有关jQuery Ajax Properties的信息.

关于jquery - 如何在 jQuery 中发出 get 和 post API 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41310556/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com