可以找到fetch和jquery ajax之间的差异

发布于 2025-01-27 22:25:54 字数 1373 浏览 2 评论 0原文

我正在尝试向私人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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毅然前行 2025-02-03 22:25:54

在jquery.ajax中,当您通过数据一个对象:

  • 它将被编码为application/x-www-form-urlencoded data
  • jQuery将包括content-类型:应用程序/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:

  • it will be encoded as application/x-www-form-urlencoded data
  • jQuery will include a Content-Type: application/x-www-form-urlencoded header

In fetch, you are passing body a string of JSON and fetch will be including a Content-Type: text/plain header.

So:

  • You are passing the wrong encoding of data
  • You are passing a Content-Type that doesn't match what the server expects or what your data actually is

Pass fetch a URLSearchParams object.

This will be encoded with the correct format and fetch will infer the correct content type from it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文