openharmony 鸿蒙 js-apis-system-fetch

2025-06-12 浏览 (1)

@system.fetch (Data Request)

NOTE

  • The APIs of this module are no longer maintained since API version 6. You are advised to use @ohos.net.http.

  • The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import fetch from '@system.fetch';

fetch.fetch3+

fetch(options:{
  url: string;
  data?: string|object;
  header?: Object;
  method?: string;
  responseType?: string;
  success?: (data: FetchResponse) => void;
  fail?: (data: any, code: number) => void;
  complete?: () => void;
} ): void

Obtains data through a network.

System capability: SystemCapability.Communication.NetStack

Parameters

NameTypeMandatoryDescription
urlstringYesResource URL.
datastring |ObjectNoRequest parameter, which can be a string or a JSON object. For details, see the mapping between data and Content-Type.
headerObjectNoRequest header.
methodstringNoRequest method. The default value is GET. The value can be OPTIONS, GET, HEAD, POST, PUT, **DELETE **or TRACE.
responseTypestringNoResponse type. The return type can be text or JSON. By default, the return type is determined based on Content-Type in the header returned by the server. For details, see return values in the success callback.
successFunctionNoCalled when the API call is successful. The return value is defined by FetchResponse.
failFunctionNoCalled when an API call fails.
completeFunctionNoCalled when an API call is complete.

Table 1 Mapping between data and Content-Type

dataContent-TypeDescription
stringLeft unspecifiedThe default value of Content-Type is text/plain, and the value of data is used as the request body.
stringAny typeThe value of data is used as the request body.
ObjectLeft unspecifiedThe default value of Content-Type is application/x-www-form-urlencoded. The data value is encoded based on the URL rule and appended in the request body.
Objectapplication/x-www-form-urlencodedThe value of data is encoded based on the URL rule and is used as the request body.

FetchResponse3+

System capability: SystemCapability.Communication.NetStack

NameTypeReadableWritableDescription
codenumberYesNoServer status code.
datastring |ObjectYesNoThe type of the returned data is determined by responseType. For details, see the mapping between responseType and data in success callback.
headersObjectYesNoAll headers in the response from the server.

Table 2 Mapping between responseType and data in success callback

responseTypedataDescription
N/AstringWhen the type in the header returned by the server is text/*, application/json, application/javascript, or application/xml, the value is the text content.
textstringText content.
jsonObjectA JSON object.

Example

export default {
  data: {
    responseData: 'NA',
    url: "test_url",
  },
  fetch: function () {
    var that = this;
    fetch.fetch({
      url: that.url,
      success: function(response) {
        console.info("fetch success");
        that.responseData = JSON.stringify(response);
      },
      fail: function() {
        console.info("fetch fail");
      }
    });
  }
}

NOTE HTTPS is supported by default. To support HTTP, you need to add "network" to the config.json file, and set the attribute "cleartextTraffic" to true, as shown below:

{
  "deviceConfig": {
    "default": {
      "network": {
        "cleartextTraffic": true
      }
      ... // Other configuration information
    }
  }
  ... // Other configuration information
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Network Kit

harmony 鸿蒙NetConn_ConnectionProperties

harmony 鸿蒙NetConn_HttpProxy

harmony 鸿蒙NetConn_NetAddr

harmony 鸿蒙NetConn_NetCapabilities

harmony 鸿蒙NetConn_NetConnCallback

harmony 鸿蒙NetConn_NetHandle

harmony 鸿蒙NetConn_NetHandleList

harmony 鸿蒙NetConn_NetSpecifier

harmony 鸿蒙NetConn_Route

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/akh9gi