XMLHttpRequest Adapter
Install
- npm
- yarn
npm install @alova/adapter-xhr --save
yarn add @alova/adapter-xhr
Instructions
create alova
Use xhrRequestAdapter as request adapter for alova.
import { createAlova } from 'alova';
import { xhrRequestAdapter } from '@alova/adapter-xhr';
const alovaInst = createAlova({
//...
requestAdapter: xhrResponseAdapter()
//...
});
Request
The XMLHttpRequest adapter provides basic configuration parameters, including responseType
, withCredentials
, mimeType
, auth
, as follows:
- vue
- react
- svelte
<tempate>
<div v-if="loading">Loading...</div>
<div>The request data is: {{ data }}</div>
</template>
<script setup>
const list = () =>
alovaInst. Get('/list', {
/**
* Set the response data type
* Can be set to change the response type. Values are: "arraybuffer", "blob", "document", "json" and "text"
* defaults to "json"
*/
responseType: 'text',
/**
* True when credentials are to be included in cross-origin requests. false when they are excluded from cross-origin requests and when cookies are ignored in their responses. Default is false
*/
withCredentials: true,
/**
* Set the mimeType of the response data
*/
mimeType: 'text/plain; charset=x-user-defined',
/**
* auth means use HTTP Basic authentication and provide credentials.
* This will set an `Authorization` header, overriding any existing
* Custom headers for `Authorization` set using `headers`.
* Note that only HTTP Basic authentication can be configured via this parameter.
* For Bearer tokens etc., use the `Authorization` custom header instead.
*/
auth: {
username: 'name1',
password: '123456'
}
});
const { loading, data } = useRequest(list);
</script>
const list = () =>
alovaInst.Get('/list', {
/**
* Set the response data type
* Can be set to change the response type. Values are: "arraybuffer", "blob", "document", "json" and "text"
* defaults to "json"
*/
responseType: 'text',
/**
* True when credentials are to be included in cross-origin requests. false when they are excluded from cross-origin requests and when cookies are ignored in their responses. Default is false
*/
withCredentials: true,
/**
* Set the mimeType of the response data
*/
mimeType: 'text/plain; charset=x-user-defined',
/**
* auth means use HTTP Basic authentication and provide credentials.
* This will set an `Authorization` header, overriding any existing
* Custom headers for `Authorization` set using `headers`.
* Note that only HTTP Basic authentication can be configured via this parameter.
* For Bearer tokens etc., use the `Authorization` custom header instead.
*/
auth: {
username: 'name1',
password: '123456'
}
});
const App = () => {
const { loading, data } = useRequest(list);
return (
{ loading ? <div>Loading...</div> : null }
<div>The request data is: { JSON.stringify(data) }</div>
)
};
<script>
const list = () =>
alovaInst.Get('/list', {
/**
* Set the response data type
* Can be set to change the response type. Values are: "arraybuffer", "blob", "document", "json" and "text"
* defaults to "json"
*/
responseType: 'text',
/**
* True when credentials are to be included in cross-origin requests. false when they are excluded from cross-origin requests and when cookies are ignored in their responses. Default is false
*/
withCredentials: true,
/**
* Set the mimeType of the response data
*/
mimeType: 'text/plain; charset=x-user-defined',
/**
* auth means use HTTP Basic authentication and provide credentials.
* This will set an `Authorization` header, overriding any existing
* Custom headers for `Authorization` set using `headers`.
* Note that only HTTP Basic authentication can be configured via this parameter.
* For Bearer tokens etc., use the `Authorization` custom header instead.
*/
auth: {
username: 'name1',
password: '123456'
}
});
const { loading, data } = useRequest(list);
</script>
{#if $loading}
<div>Loading...</div>
{/if}
<div>The request data is: { data }</div>
Upload
Use FormData
to upload files, and this FormData
instance will be sent to the server through xhr.send
. It will be set Content-Type
automatically, you don't need to custom it with multipart/form-data
.
const uploadFile = imageFile => {
const formData = new FormData();
formData.append('file', imageFile);
return alovaInst.Post('/uploadImg', formData, {
// Start upload progress
enableUpload: true
});
};
const {
loading,
data,
uploading,
send: sendUpload
} = useRequest(uploadFile, {
immediate: false
});
// Picture selection event callback
const handleImageChoose = ({ target }) => {
sendUpload(target.files[0]);
};