Home > Web Front-end > Front-end Q&A > javascript put request parameters

javascript put request parameters

WBOY
Release: 2023-05-22 13:12:08
Original
2220 people have browsed it

With the continuous development of front-end technology, JavaScript has become an indispensable part of Web development. In some scenarios, we need to use PUT requests to update data on the server. In this article, we will explore how to send parameters using PUT request in JavaScript.

PUT request is an update request for server resources. Compared with the POST request, the PUT request can replace or update the specified resource without specifying the complete URL. The syntax rules of the PUT request are as follows:

PUT /resource HTTP/1.1
Host: localhost
Content-Type: application/json
Content-Length: <length>

{
    "name": "put request",
    "description": "update server data"
}
Copy after login

Among them, Content-Type and Content-Length are used to specify the requested data format and data length. In JavaScript, we can use the XMLHttpRequest object to make PUT requests. The following is a sample code that uses the XMLHttpRequest object to send a PUT request:

const xhr = new XMLHttpRequest();
xhr.open("PUT", "/resource");
xhr.setRequestHeader("Content-Type", "application/json");

const data = {
    "name": "put request",
    "description": "update server data"
};

xhr.send(JSON.stringify(data));
Copy after login

Here xhr is the XMLHttpRequest object, and the open method is used to configure the requested URL and request method. , The setRequestHeader method is used to set the Content-Type request header to specify the requested data format. The send method is used to send a request and convert the request parameters into a JSON string.

In addition to using the XMLHttpRequest object to send a PUT request, we can also use the fetch API. In ES6, modern browsers already natively support the fetch API. The following is a sample code that uses the fetch API to send a PUT request:

const options = {
    method: "PUT",
    headers: {
        "Content-Type": "application/json"
    },
    body: JSON.stringify({
        "name": "put request",
        "description": "update server data"
    })
};

fetch("/resource", options)
    .then(response => console.log(response))
    .catch(error => console.log(error));
Copy after login

The options parameters here are used to configure the request method, request headers and request parameters. The fetch method is used to send a request and return a Promise object. We can use the .then and .catch methods to handle the response and errors of the request.

In summary, sending a PUT request in JavaScript is very simple. Just use the XMLHttpRequest object or the fetch API to convert the request parameters into a JSON string and configure the request header.

The above is the detailed content of javascript put request parameters. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template