Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react post request

componentDidMount() {
    // Simple POST request with a JSON body using fetch
    const requestOptions = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ title: 'React POST Request Example' })
    };
    fetch('https://jsonplaceholder.typicode.com/posts', requestOptions)
        .then(response => response.json())
        .then(data => this.setState({ postId: data.id }));
}
Comment

how to send a post request with react

  const handler = () => {
        const requestOptions = {
          mode: 'no-cors' as RequestMode,
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ "logo": "t-shirt" })
          };
          fetch('http://localhost:8080/tshirt/19', requestOptions)
              .then(response => response.json())
              .then(data => setApiData({ postId: data.id }));
        }
Comment

How to use Post Method in react

import axios from "axios";
import React from "react";

const baseURL = "https://jsonplaceholder.typicode.com/posts";

export default function App() {
  const [post, setPost] = React.useState(null);

  React.useEffect(() => {
    axios.get(`${baseURL}/1`).then((response) => {
      setPost(response.data);
    });
  }, []);

  function createPost() {
    axios
      .post(baseURL, {
        title: "Hello World!",
        body: "This is a new post."
      })
      .then((response) => {
        setPost(response.data);
      });
  }

  if (!post) return "No post!"

  return (
    <div>
      <h1>{post.title}</h1>
      <p>{post.body}</p>
      <button onClick={createPost}>Create Post</button>
    </div>
  );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js replace all number 
Javascript :: capture image from video element 
Javascript :: react native community eslint 
Javascript :: how to get cwd nodejs 
Javascript :: javascript current date 
Javascript :: node.js mysql create table 
Javascript :: react-dom.development.js:86 Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. 
Javascript :: es6 js slug from string 
Javascript :: moment timezone get offset from iana timezone 
Javascript :: onchange event angular select 
Javascript :: checkbox is checked jquery 
Javascript :: map through keys javascript 
Javascript :: replace - with space 
Javascript :: console.log red text on yellow background 
Javascript :: javascript append how first element 
Javascript :: jquery create a button 
Javascript :: how to change specific object in array javascript 
Javascript :: jquery clone and append 
Javascript :: how to add attribute to selected element in javascript 
Javascript :: jetbrains font vscode 
Javascript :: Return the Next Number from the Integer Passed javascript 
Javascript :: draw text in js 
Javascript :: javascript get random character from string 
Javascript :: foreach jquery 
Javascript :: make string json object vue 
Javascript :: get element by xpath in javascript 
Javascript :: js speech synthesis 
Javascript :: add firebase angular 
Javascript :: jquery confirm delete massege 
Javascript :: regex diferent of 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =