osyris zosar

osyris zosar

  • NA
  • 289
  • 24.3k

Fetich ReactJS with Asp net core

Apr 30 2021 8:38 PM
I am trying to understand fetching in Reactjs
I simply want to pass a string value to my React project

Please help me out
 
Controller: 
 
  1. namespace reacttest.Controllers  
  2. {  
  3.   
  4.     public class TestingController : Controller  
  5.     {  
  6.         [HttpGet("New")]  
  7.         public string New()  
  8.         {  
  9.             string Name = "Jon";  
  10.             return Name;  
  11.         }  
  12.     }  
  13. }  
 
 ReactJS:
 
  1. import { data } from 'jquery';  
  2. import React, { Component } from 'react';  
  3.   
  4. export class New extends Component {  
  5.     static displayname = New.name;  
  6.   
  7.     constructor(props) {  
  8.         super(props);  
  9.         this.state = {  
  10.             Names1 : "nothing"  
  11.         };  
  12.     }  
  13.     componentDidMount() {  
  14.         fetch('reacttest/TestingController/New')  
  15.             .then(Response => (Response.json())  
  16.             .then(data => { this.setState({ Name1: data }) })  
  17.     }  
  18.   
  19.     render() {  
  20.         return (  
  21.             <h1>my name = {this.state.Names1} </h1>  
  22.             )  
  23.     }  
  24. }  
 it just returns "Nothing"

Answers (1)