0

I want to bring data from another table to use in detail view. But the value returned from the getData method is null. What should i do ?

<th data-field="id" data-detail-formatter="DetailFormatter">ID</th>
<th data-field="prefix" data-detail-formatter="DetailFormatter">Prefix</th>
<th data-field="fname" data-detail-formatter="DetailFormatter"Name</th>

function DetailFormatter(index, row) {
   return '<p> ID: ' + row.id + '</p>',
          '<p> Name: ' + row.prefix + '</p>',
          '<p> Price: ' + row.fname + '</p>',
          '<p> Degree: ' + getData() + '</p>'
}

function getData() {
   var data

   $.getScript('http://localhost:55289/ManageTeacher/GetTacherDegree', function (script, status, jqxhr) {
      data = script
   });

   return data
}
Tee Adkins
  • 43
  • 4

2 Answers2

0

It is a kind of asynchronous process. You are able to get correct value inside callback function. So you can move the codes that handles data inside callback function instead of returning data.

function getData() {
   var data

   $.getScript('http://localhost:55289/ManageTeacher/GetTacherDegree', function (script, status, jqxhr) {
      data = script.  // <--------------- right place
   });

   return data // <------------------- wrong place
}
Prime
  • 2,809
  • 1
  • 7
  • 23
0

Try using fetch api inside an async function

async function getData() {

const data = await fetch('http://localhost:55289/ManageTeacher/GetTacherDegree')
.then(res => res.json())

 return data
 }
ptothep
  • 415
  • 3
  • 10