js-apis-system-fetch.md 4.9 KB
Newer Older
G
Gloria 已提交
1
# @system.fetch (Data Request)
Z
zengyawen 已提交
2

G
Gloria 已提交
3
> **NOTE**
S
shawn_he 已提交
4
> - The APIs of this module are no longer maintained since API version 6. You are advised to use [`@ohos.net.http`](js-apis-http.md) instead.
S
shawn_he 已提交
5
> 
Z
zengyawen 已提交
6 7 8 9 10 11 12 13 14 15 16
> - 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';
```


S
shawn_he 已提交
17
## fetch.fetch<sup>3+</sup>
Z
zengyawen 已提交
18

S
shawn_he 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
fetch(options:{
    /**
     * Resource URL.
     * @since 3
     */
    url: string;
    /**
     * Request parameter, which can be of the string type or a JSON object.
     * @since 3
     */
    data?: string | object;
    /**
     * Request header, which accommodates all attributes of the request.
     * @since 3
     */
    header?: Object;
    /**
     * Request methods available: OPTIONS, GET, HEAD, POST, PUT, DELETE and TRACE. The default value is GET.
     * @since 3
     */
    method?: string;
    /**
     * 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.
     * @since 3
     */
    responseType?: string;
    /**
     * Called when the network data is obtained successfully.
     * @since 3
     */
    success?: (data: FetchResponse) => void;
    /**
     * Called when the network data fails to be obtained.
     * @since 3
     */
    fail?: (data: any, code: number) => void;
    /**
     * Called when the execution is completed.
     * @since 3
     */
    complete?: () => void;
  } ): void
Z
zengyawen 已提交
61 62 63 64 65 66

Obtains data through a network.

**System capability**: SystemCapability.Communication.NetStack

**Parameters**
S
shawn_he 已提交
67
| Name| Type| Mandatory| Description|
Z
zengyawen 已提交
68
| -------- | -------- | -------- | -------- |
S
shawn_he 已提交
69 70 71 72 73
| url | string | Yes| Resource URL.|
| data | string \| Object | No| Request parameter, which can be a string or a JSON object. For details, see the mapping between **data** and **Content-Type**.|
| header | Object | No| Request header.|
| method | string | No| Request method. The default value is **GET**. The value can be **OPTIONS**, **GET**, **HEAD**, **POST**, **PUT**, **DELETE **or **TRACE**.|
| responseType | string | No| Response 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.|
S
shawn_he 已提交
74 75 76
| success | Function | No| Called when the API call is successful. The return value is defined by [FetchResponse](#fetchresponse).|
| fail | Function | No| Called when API call has failed.|
| complete | Function | No| Called when the API call is complete.|
S
shawn_he 已提交
77 78 79 80

**Table 1** Mapping between data and Content-Type

| data | Content-Type | Description|
Z
zengyawen 已提交
81
| -------- | -------- | -------- |
S
shawn_he 已提交
82 83 84 85
| string | Not set| The default value of Content-Type is **text/plain**, and the value of data is used as the request body.|
| string | Any type| The value of data is used as the request body.|
| Object | Not set| The 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.|
| Object | application/x-www-form-urlencoded | The value of data is encoded based on the URL rule and is used as the request body.|
Z
zengyawen 已提交
86

S
shawn_he 已提交
87 88 89
## FetchResponse<sup>3+</sup>

**System capability**: SystemCapability.Communication.NetStack
Z
zengyawen 已提交
90

S
shawn_he 已提交
91 92 93 94 95
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| code | number | Yes| No| Server status code.|
| data | string \| Object | Yes| No| The type of the returned data is determined by **responseType**. For details, see the mapping between **responseType** and **data** in **success** callback.|
| headers | Object | Yes| No| All headers in the response from the server.|
Z
zengyawen 已提交
96

S
shawn_he 已提交
97
**Table 2** Mapping between responseType and data in success callback
Z
zengyawen 已提交
98

S
shawn_he 已提交
99
| responseType | data | Description|
Z
zengyawen 已提交
100
| -------- | -------- | -------- |
S
shawn_he 已提交
101 102 103
| N/A| string | When the type in the header returned by the server is **text/\***, **application/json**, **application/javascript**, or **application/xml**, the value is the text content.|
| text | string | Text content.|
| json | Object | A JSON object.|
Z
zengyawen 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

**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");
      }
    });
  }
}
```


S
shawn_he 已提交
130
> **NOTE**
S
shawn_he 已提交
131
>   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**. That is:
Z
zengyawen 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144
>   
> ```
> {
>   "deviceConfig": {
>     "default": {
>       "network": {
>         "cleartextTraffic": true
>       }
>       ...
>     }
>   }
>   ...
> }
S
shawn_he 已提交
145
> ```