0

I am working with Reactjs and i am using Nextjs,I am trying to Post data with axios But Data is inserting "NULL" values,Where i am wrong ? Here is my code in nextjs

const userData = {
     name: state.name,  
      job: state.job
};
axios.post("https://diggdevelopment.com/openc/api/testinfo", userData).then((response) => {
        alert(response);
      });
  

Here is my code in php

$_POST = json_decode(file_get_contents("php://input"),true);
$name=$_POST['name'];
$job=$_POST['job'];
$data = array(
       'name'=>$name,
        'job'=>$job
       );
$this->db->insert('test',$data);
15
  • Put the await before axios.post, may be your call is taking time. Commented Nov 11, 2022 at 8:10
  • @ShahzadKhan Please explain your point sir, didn't understand Commented Nov 11, 2022 at 8:12
  • He said you should make it this way: let response = await axios.post("https://diggdevelopment.com/openc/api/testinfo", userData); then alert(response); Commented Nov 11, 2022 at 8:15
  • 1
    hard to say with such a small sample but i suspect your state.name or state.job is null. Check what axios i actually posting. otherwise there is a problem on the receiving end Commented Nov 11, 2022 at 8:15
  • 1
    @IbrahimHammed why tough? using the promise with .then should suffice Commented Nov 11, 2022 at 8:16

1 Answer 1

0
const userData = JSON.stringify({ 
     name: state.name,  
      job: state.job
});  

axios.post("https://diggdevelopment.com/openc/api/testinfo", userData).then((response) => {
        alert(response);
      });
Sign up to request clarification or add additional context in comments.

1 Comment

@amit should be JSON.stringify

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.