如何将Fetch GET命令与react js中的params一起使用



我想在react js 中的fetch命令中使用这个ajax命令

var data = {
resource_id: '5f5afc43-639a-4216-8286-d146a8e048fe', // the resource id
};
$.ajax({
url: 'https://data.gov.il/api/action/datastore_search',
data: data,
dataType: 'json',
success: function (data) {
console.log(data);
}
});

如何在Fetch命令中使用该命令?

下面是一个关于如何使用fetch的示例,这个示例使用JSON占位符

fetch("https://jsonplaceholder.typicode.com/todos/1", {
headers: { ContentType: "application/json" },
})
.then((response) => response.json())
.then(console.log);

对于您的代码,它可能是这样的,但不幸的是,您请求的API需要访问令牌,因此它没有经过测试:

var data = {
resource_id: '5f5afc43-639a-4216-8286-d146a8e048fe', // the resource id
};
fetch("https://data.gov.il/api/action/datastore_search", {
headers: { ContentType: "application/json" },
body: JSON.stringify(data);
})
.then((response) => response.json())
.then(console.log);

fetch('https://data.gov.il/api/action/datastore_search', {
headers: {'ContentType': 'application/json'},
body: JSON.stringify(data)
}).then(response => response.json()).then(console.log)

我成功了,我只是把ajax命令放在react项目中,它就在中工作了

最新更新