Advertisement
Guest User

Untitled

a guest
Aug 10th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import { Button, FormGroup, FormControl, ControlLabel } from "react-bootstrap";
  3. import "./Login.css";
  4. import axios from 'axios';
  5.  
  6.  
  7. //fetch('http://localhost:52654/Service1.svc/Login', {
  8. // method: 'POST',
  9. // headers: {
  10. // 'Accept': 'application/json',
  11. // 'Content-Type': 'application/json',
  12. // },
  13. // body: JSON.stringify({
  14. // user: ' ' ,
  15. // password: ' ' ,
  16. // })
  17. //})
  18.  
  19. const API = 'http://localhost:52654/Service1.svc/Login';
  20.  
  21. export default class Login extends Component {
  22. constructor(props) {
  23. super(props);
  24.  
  25. this.state = {
  26. user: "",
  27. password: ""
  28. }
  29. /* this.fetchuser = this.fetchuser.bind(this);*/
  30. //this.handleSubmit = this.handleSubmit.bind(this)
  31. //this.onChange = this.onChange.bind(this);
  32. //this.onClick = this.onClick.bind(this);
  33. }
  34.  
  35. /*--*/
  36.  
  37. /* fetchuser = () => {
  38. axios.get('http://localhost:52654/Service1.svc/Login/')
  39. .then((response) => {
  40. this.setState({ user: response.data, password: response.data })
  41. .cath((error) => {
  42.  
  43. console.log(error);
  44. });
  45. }
  46.  
  47. }*/
  48.  
  49.  
  50.  
  51.  
  52. validateForm() {
  53. return this.state.user.length > 0 && this.state.password.length > 0;
  54. }
  55.  
  56. handleChange = event => {
  57. this.setState({
  58. [event.target.id]: event.target.value
  59. });
  60.  
  61. };
  62.  
  63. handleSubmit = event => {
  64. axios({
  65. method: 'post',
  66. url: API,
  67. data: {
  68. user: this.state.user,
  69. password: this.state.password
  70. },
  71. withCredentials: false,
  72. responseType: 'json',
  73. responseEncoding: 'utf8'
  74. }).then(function (response) {
  75. console.log(response);
  76. });
  77.  
  78. };
  79.  
  80. handleClick() {
  81. //axios.post(API, {
  82. // user: this.state.user,
  83. // password: this.state.password
  84. //}).then(function (response) {
  85. // console.log(response);
  86. //}).catch(function (error) {
  87. // console.log(error.response);
  88. //});
  89.  
  90. axios({
  91. method: 'post',
  92. url: API,
  93. data: {
  94. user: this.state.user,
  95. password: this.state.password
  96. },
  97. withCredentials: false,
  98. responseType: 'json',
  99. responseEncoding: 'utf8'
  100. }).then(function (response) {
  101. console.log(response);
  102. });
  103. }
  104.  
  105.  
  106.  
  107.  
  108. render() {
  109. return (
  110. <div className="Login">
  111. <form onSubmit={this.handleSubmit.bind(this)}>
  112. <FormGroup controlId="user" bsSize="large">
  113. <ControlLabel>Username</ControlLabel>
  114. <FormControl
  115. autoFocus
  116. type="user"
  117. value={this.state.user}
  118. onChange={this.handleChange.bind(this)}
  119. />
  120. </FormGroup>
  121. <FormGroup controlId="password" bsSize="large">
  122. <ControlLabel>Password</ControlLabel>
  123. <FormControl
  124. value={this.state.password}
  125. onChange={this.handleChange.bind(this)}
  126. type="password"
  127. />
  128. </FormGroup>
  129. <Button
  130. block
  131. bsSize="large"
  132. disabled={!this.validateForm()}
  133. //onClick={this.handleClick.bind(this)}
  134. type="submit"
  135. >
  136. Login
  137. </Button>
  138. </form>
  139. </div>
  140. );
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement