Ajax with Json Data in Vanilla JavaScript

  let setId = id;

        let data = {id:setId};

        const jsonToString = JSON.stringify(data);

   const xhr = new XMLHttpRequest();

        xhr.open('POST','https://eteacherbd.com/app-functions/english/build-vocabulary/get-text.php', true);

        xhr.setRequestHeader('Content-type', 'application/json');

  xhr.onreadystatechange = function () {

   if(this.status === 200 ){

       const Data = JSON.parse(this.responseText);

        getid.innerHTML = Data.id;

       getWord.innerHTML = Data.word;

      getMeaning.innerHTML = Data.meaning;

    }

};

xhr.send(jsonToString);


Access in php file 

<?php

 header("Content-Type: application/json");

  $jsonData = file_get_contents('php://input');

    $data = json_decode($jsonData);

       if($data->id){

          $id =$data->id;

       }else{

          $id = 1;

       }

Comments

Popular posts from this blog

Battery discharging formula

HTML CSS JS Animated Sidebar Menu