Response: Response() constructor
{{APIRef("Fetch API")}}{{AvailableInWorkers}}
The Response() constructor creates a new {{domxref("Response")}} object.
Syntax
new Response()
new Response(body)
new Response(body, options)
Parameters
-
body{{optional_inline}}- : An object defining a body for the response. This can be
null(which is the default value), or one of:- {{domxref("Blob")}}
- {{jsxref("ArrayBuffer")}}
- {{jsxref("TypedArray")}}
- {{jsxref("DataView")}}
- {{domxref("FormData")}}
- {{domxref("ReadableStream")}}
- {{domxref("URLSearchParams")}}
- {{jsxref("String")}}
- string literal
- : An object defining a body for the response. This can be
-
options{{optional_inline}}- : An options object containing any custom settings that you want to apply to the response, or an empty object (which is the default value). The possible options are:
status- : The status code for the response.
The default value is
200.
- : The status code for the response.
The default value is
statusText- : The status message associated with the status code, such as
"OK". The default value is"".
- : The status message associated with the status code, such as
headers- : Any headers you want to add to your response, contained within a {{domxref("Headers")}} object or object literal of {{jsxref("String")}} key/value pairs (see HTTP headers for a reference). By default this is empty.
- : An options object containing any custom settings that you want to apply to the response, or an empty object (which is the default value). The possible options are:
Examples
In our Fetch Response example (see Fetch Response live)
we create a new Response object using the constructor, passing it a new {{domxref("Blob")}} as a body, and an init object containing a custom status and statusText:
const myBlob = new Blob();
const myOptions = { status: 200, statusText: "SuperSmashingGreat!" };
const myResponse = new Response(myBlob, myOptions);
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
这篇文档对你有帮助吗?
谢谢你的反馈!