logo
down
shadow

AXIOS returning an empty array on ajax request


AXIOS returning an empty array on ajax request

By : Arno Seneqerimyan
Date : October 17 2020, 03:08 PM
hope this fix your issue you have to get the param from the url in your controller. update your The VitrineController.php like blow.
code :
public function trocaCarros($id, Request $request)
{
    $marca = $id;
    $umaMarcaModelo = Modelo::where('marca_id', $marca)->get();
    return response()->json($umaMarcaModelo);
} 


Share : facebook icon twitter icon
.net core 2.1 binding array in ajax request from axios and Vue.js

.net core 2.1 binding array in ajax request from axios and Vue.js


By : Tyjeik Hughes
Date : March 29 2020, 07:55 AM
it should still fix some issue The reason the binding is failing is due to the extra [] on the end of ratings in the query-string parameters. ASP.NET Core will expect this to be simply ratings (not ratings[]). One option for making this work is to inform ASP.NET Core that the name is in fact ratings[], which can be done by using the FromQuery attribute with a Name property, like this:
code :
public IActionResult Get(float lat, float lng, int radius, int fetch,
    [FromQuery(Name="ratings[]")] List<int> ratings)
{
    ...
}
axios put request in react is returning empty

axios put request in react is returning empty


By : user3002595
Date : March 29 2020, 07:55 AM
it should still fix some issue I am assuming it is because of the way you are handling the onchange of your inputs. You want to set the onchange to the client value in your state. But instead you are setting it to the state itself. So then when you are building your object to send to the backend you are sending null data because you haven't set any data to the actual client value in your state and it is still null. Try console logging the state and you will see what I'm talking about. Also you are adding a .value to the end each of the state values you are trying to build your object with and this is not necessary. Finally you don't need to specify an onchange for each input just give the input a name attribute and you can set your onchange handler like so:
code :
onChange = e => {
  this.setState({
    [e.target.name]: e.target.value
  })
}
import React, { Component } from 'react';
import axios from 'axios';
import API from '../../utils/API';

class index extends Component {

constructor(props) {
    super(props);

    this.onChange = this.onChange.bind(this);

    this.onSubmit = this.onSubmit.bind(this);
    this.state = {
        client: null
    }

}

componentDidMount() {

    let id = this.props.match.params.id
    API.getClient(id)
        .then(res => {
            this.setState({
                client: res.data
            })
            console.log(this.state.client.id)
        })
        .catch(error => console.log(error))
}


onChange(e) {
    this.setState({
        client: {
             ...this.state.client,
             [e.target.name]: e.target.value
        }
    });
}


onSubmit(e) {
    e.preventDefault();

    let obj = {
        lastName: this.state.client.lastName,
        firstName: this.state.client.firstName,
        phone: this.state.client.phone,
        petName: this.state.client.petName,
        breed: this.state.client.breed,
        notes: this.state.client.notes
    };

    let id = this.state.client.id

    axios.put("http://localhost:3000/api/clients/" + id, obj)

        // .then(alert("client Updated"))
        .then(res => console.log(res))
        .catch(error => console.log(error))

    this.props.history.push('/admin');
}

render() {

    const client = this.state.client ? (
        <div className="client">
            <h3 style={{ marginLeft: "60px" }}>Update Client</h3>

            <form onSubmit={this.onSubmit} style={{ padding: "60px" }}>
                <div className="form-group">
                    <label>Last Name: </label>
                    <input type="text"
                        name="lastName"
                        className="form-control"
                        defaultValue={this.state.client.lastName}
                        onChange={this.onChange}
                    />
                </div>
                <div className="form-group">
                    <label>First Name: </label>
                    <input type="text"
                        name="firstName"
                        className="form-control"
                        defaultValue={this.state.client.firstName}
                        onChange={this.onChange}
                    />
                </div>
                <div className="form-group">
                    <label>Phone: </label>
                    <input type="text"
                        name="phone"
                        className="form-control"
                        defaultValue={this.state.client.phone}
                        onChange={this.onChange}
                    />
                </div>
                <div className="form-group">
                    <label>Pet Name: </label>
                    <input type="text"
                        name="petName"
                        className="form-control"
                        defaultValue={this.state.client.petName}
                        onChange={this.onChange}
                    />
                </div>
                <div className="form-group">
                    <label>Breed: </label>
                    <input type="text"
                        name="breed"
                        className="form-control"
                        defaultValue={this.state.client.breed}
                        onChange={this.onChange}
                    />
                </div>
                <div className="form-group">
                    <label>Notes: </label>
                    <input type="text"
                        name="notes"
                        className="form-control"
                        defaultValue={this.state.client.notes}
                        onChange={this.onChange}
                    />
                </div>

                <br />
                <div className="form-group">
                    <input type="submit" value="Update Client" 
                     className="btn btn-primary" />
                </div>
            </form>

        </div>
    ) : (
            <div className="center">Loading Client</div>
        )

    return (


            <div className="container">
                {client}
            </div>
        )
    }
}

export default index;
Axios returning empty array before returning the data

Axios returning empty array before returning the data


By : user3074240
Date : March 29 2020, 07:55 AM
will help you This is because of the asynchronous behaviour of the react life cycles . Your render is called before the completion of your axios request and this.state.Metaproducts was never updated inside render as it was already rendered before your request . You can wait for the callback from the axios and then update the state only after that and make sure to render the product after the completion of ajax.
I have added one more state variable init , and set that as true after getting response from server . By default init will be false and then you can show the "loading message " .
code :
import React, { Component } from 'react';
import axios from 'axios';

class Products extends Component {
  state = {
    MetaProducts: [],
    init:0,
  }

  async componentDidMount() {
   await axios.get('https://mobile-tha-server-8ba57.firebaseapp.com/walmartproducts/1/20').then(res => {
      this.setState({ MetaProducts: res.data.products, init:1 });
    });
  }

  renderProduct() {
    return <ul>
    {this.state.MetaProducts.map(p =>
      <li>
        {p.productName}
      </li>
    )};
    </ul>;
  }

  render() {
    return this.state.init ? <div>{this.renderProduct()}</div> : <div>loading...</div>;
   }
}



export default Products;
Axios 'put' request passing array as empty value

Axios 'put' request passing array as empty value


By : lakerz
Date : March 29 2020, 07:55 AM
Hope this helps This is my 'PUT' request using Axios. , Try this
code :
 axios("URL", {
    method: "PUT",
    headers: {
      "content-type": "application/json"
    },
    data: loanData
  })
    .then(response => {
      console.log("Successfully Updated !");

    })
    .catch(err => {
      console.log(err.response);

    });
React js and axios POST request sends empty array

React js and axios POST request sends empty array


By : tripnikk
Date : March 29 2020, 07:55 AM
seems to work fine When I try to post to the other server using axios it first sends OPTIONS request then POST. When I check network connection it sends object with fields I wanted just the this.state.array is empty. , Solved
myArray was defined as array under state myArray : []
Related Posts Related Posts :
  • Live Program Accessing Local Files
  • Laravel - General error: 1366 Incorrect integer value
  • mongodb find query with OR and AND
  • Move Array element using PHP/Laravel
  • How to add new value in collection laravel?
  • PHP Error : Fatal error: Constant expression contains invalid operations
  • Get date of next thursday (including today if we are thursday)
  • PHP Regexp with dash and ampersand doesn't work
  • In MySQL how do I edit multiple records with the same value at the same time?
  • My HTML form only returns 1 value into the PHP mail
  • Create an adjacency structure from a list of sequence
  • PHP Arrays - Merge two arrays where a value is added together
  • Laravel 5.2 persistent session id through login, ok to use _token?
  • syntax error, unexpected 'if' (T_IF),how to resolve
  • nginx 403 forbidden error + mac + laravel
  • Find out whether string has date in it
  • Get values of radio selected values of entire row to next file
  • php preg_replace function wrong result
  • Checkbox not selected based on database value
  • Remove internal array in php
  • How to automatically refresh a page and redirects to login page when session has expired (PHP)
  • Trying to Link a populated drop down list PHP
  • Associate Array Sorting
  • Show Tags with Link from Word String
  • Symfony 3 Dynamic Generation for Submitted Forms not working
  • I want to change the value of a string
  • How to remove category name from url in opencart 2.x?
  • Undefined property: PDOStatement::,$id_login,$nama_depan
  • Getting base path in URL
  • React / Laravel (php) SPA on same (Apache) server
  • In PHP, how can we define a field as a certian class's instance like JAVA?
  • Don't work correctly when clicking on stars
  • php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not knownfailed?
  • Change php to can select the html in css
  • PHP import CSV with umlauts
  • Populate Select based on another Select
  • How to save value after each click with ajax
  • PHP sort array by date and character
  • Duplicate entry for key 'PRIMARY' in mysql code
  • unlink() doesn't work with absolute path
  • DOMXPath - Get Node
  • jQuery - storing variables from PHP foreach loop
  • In Woocommerce, how to use a legacy API instead of the latest version?
  • Mapping home controller as the default controller in AltoRouter
  • Correspondency model - table on CodeIgniter 2
  • Laravel do not validate if field is not required
  • Bootstrap 4.0.0-beta modal won't work. Popper.js is breaking
  • SOAP PHP : how to translate request file to PHP function call
  • Anchor Link to WooCommerce Reviews
  • Fill Drop down On select Of Drop down
  • GoDaddy php.ini on shared cpanel hosting... where, what and how?
  • rules() function in Laravel Request doesn't create unique slugs
  • MySQL query results is not being Inserted into an option list
  • Counting Rows based on Identifier - Laravel
  • Convert small bash command to php
  • Laravel - Can't find an image, though it is there in the disk. Can't reach it even through typing the URL
  • PHP PDO only last value of array gets inserted using bindValue & bindParam
  • I need to load the distance data from Google Maps and save it to my Database
  • Resizing bootstrap form box not working when <span> tag is removed
  • How to take all the words in one normal array from the database in the Laravel framework with a minimum waste of time?
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com