可以找到fetch和jquery ajax之间的差异
我正在尝试向私人API(托管ITOP)执行API请求,在Doc中有一个示例与Jquery Ajax一起使用,但我也用fetch拨打了所有其他电话,我也想这样做,但我没有得到正确的回应。
获取方法返回了我的代码200,但使用HTML而不是JSON(与JQuery Ajax JSON完全不同的内容)。
这是两个功能:
// Code made by myself - don't get the right response
fetch(url, {
method: "POST",
headers: {
"accept": "application/json",
},
mode: "cors",
accept: "application/json, text/javascript, *!/!*; q=0.01",
body: JSON.stringify({
auth_user: user,
auth_pwd: password,
json_data: JSON.stringify({
"operation": "list_operations",
})
})
})
.then(response => response.json())
.then(data => {
console.log(data);
})
// Code from the doc example - working
$.ajax({
type: "POST",
url: url,
dataType: "json",
data: {
auth_user: user,
auth_pwd: password,
json_data: JSON.stringify({
"operation": "list_operations"
})
},
crossDomain: 'true'
})
.then(
function(data, textStatus, jqXHR) {
console.log(data);
console.log(textStatus);
console.log(jqXHR);
},
function(jqXHR, textStatus, errorThrown) {
console.debug(jqXHR);
console.log("ERROR !!\n" +
"status: " + textStatus + " (" + jqXHR.status + ")\n" +
"error: " + errorThrown);
}
);
I'm trying to do API request to a private API (hosted Itop), there's an example in the doc with JQuery Ajax but I did all my others calls with fetch and I wanted to do this one too but I don't get the right response.
The fetch method returns me a code 200 but with HTML instead of JSON (with a totally different content from the jQuery AJAX JSON one).
Here's the 2 functions:
// Code made by myself - don't get the right response
fetch(url, {
method: "POST",
headers: {
"accept": "application/json",
},
mode: "cors",
accept: "application/json, text/javascript, *!/!*; q=0.01",
body: JSON.stringify({
auth_user: user,
auth_pwd: password,
json_data: JSON.stringify({
"operation": "list_operations",
})
})
})
.then(response => response.json())
.then(data => {
console.log(data);
})
// Code from the doc example - working
$.ajax({
type: "POST",
url: url,
dataType: "json",
data: {
auth_user: user,
auth_pwd: password,
json_data: JSON.stringify({
"operation": "list_operations"
})
},
crossDomain: 'true'
})
.then(
function(data, textStatus, jqXHR) {
console.log(data);
console.log(textStatus);
console.log(jqXHR);
},
function(jqXHR, textStatus, errorThrown) {
console.debug(jqXHR);
console.log("ERROR !!\n" +
"status: " + textStatus + " (" + jqXHR.status + ")\n" +
"error: " + errorThrown);
}
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在jquery.ajax中,当您通过
数据
一个对象:application/x-www-form-urlencoded
datacontent-类型:应用程序/x-www-form-urlencoded
fetch中的标题,您正在传递
body
a json 的字符串和获取将包括内容类型:text/plain
标题。因此:
通过
fetch
a < a href =“ https://developer.mozilla.org/en-us/docs/web/api/urlsearchparams” rel =“ nofollow noreferrer”>urlsearchparams
object。这将使用正确的格式编码,
fetch
将从中推断出正确的内容类型。In jQuery.ajax, when you pass
data
an object:application/x-www-form-urlencoded
dataContent-Type: application/x-www-form-urlencoded
headerIn fetch, you are passing
body
a string of JSON and fetch will be including aContent-Type: text/plain
header.So:
Pass
fetch
aURLSearchParams
object.This will be encoded with the correct format and
fetch
will infer the correct content type from it.