0

I am trying to get data from MySQL with ajax and here is my code

function Testing()
{
    var text;
    var langData=[];
    
    
    $.ajax({
    type: "POST",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    url: 'http://localhost/Android/Project/Edu/api/language/read.php',
    
    success:function (data) {
        
        langData = JSON.stringify(data);
        
        
        for (var i in langData) 
        {
           console.log("row " + i);
           for (var j in langData[i]) 
             {
                 text = text + " " + langData[i][j];
             }
        }
        
        document.getElementById('LanguageCode').value=text;
    },

    error: function () {
        alert('Failed');
    }
    
    });
}

and i got the result as the following

undefined {
    " r e c o r d s ": [{
        " L a n g u a g e I D ": " 1 ",
        " L a n g u a g e N a m e ": " E n g l i s h ",
        " L a n g u a g e C o d e ": " E n g ",
        " P r i o r i t y ": " 1 ",
        " R e g D a t e ": " 2 0 2 0 - 0 7 - 2 9 ",
        " S t a t u s ": " A c t i v e "
    }, {
        " L a n g u a g e I D ": " 2 ",
        " L a n g u a g e N a m e ": " C h i n e s e ",
        " L a n g u a g e C o d e ": " c n ",
        " P r i o r i t y ": " 0 ",
        " R e g D a t e ": " 2 0 2 0 - 0 8 - 0 1 ",
        " S t a t u s ": " A c t i v e "
    }, {
        " L a n g u a g e I D ": " 3 ",
        " L a n g u a g e N a m e ": " J a p a n e s e ",
        " L a n g u a g e C o d e ": " j p ",
        " P r i o r i t y ": " 0 ",
        " R e g D a t e ": " 2 0 2 0 - 0 8 - 0 1 ",
        " S t a t u s ": " A c t i v e "
    }]
}

all words / columns name got spacing and i want to create html table using that return data.

how do i Fix it?

3
  • 2
    give value for text=""; before ajaxcall or before 1st for loop.... then you won't receive as undefined Commented Aug 1, 2020 at 7:11
  • 3
    I am not even sure why you are using JSON.stringify(data); in your data and your expected dataType as json in ajax Commented Aug 1, 2020 at 7:14
  • 1
    Why are you using JSON.stringify, langData = JSON.stringify(data);? Commented Aug 1, 2020 at 7:17

1 Answer 1

1

Try like this.. with your ajax json response...

function Testing()
{
    var text='';
    var langData={"records":[{"LanguageID":"1","LanguageName":"English","LanguageCode":"Eng","Priority":"1","RegDate":"2020-07-29","Status":"Active"},{"LanguageID":"2","LanguageName":"Chinese","LanguageCode":"cn","Priority":"0","RegDate":"2020-08-01","Status":"Active"},{"LanguageID":"3","LanguageName":"Japanese","LanguageCode":"jp","Priority":"0","RegDate":"2020-08-01","Status":"Active"}]};
        for (var i in langData.records) {
                 text = text +'<tr><td>'+langData.records[i].LanguageID+'</td><td>'+langData.records[i].LanguageName+'</td><td>'+langData.records[i].LanguageCode+'</td><td>'+langData.records[i].Priority+'</td><td>'+langData.records[i].RegDate+'</td><td>'+langData.records[i].Status+'</td></tr>';
        }        document.getElementById('LanguageCode').innerHTML=text;
}
Testing();
<table>
<thead><tr><th>LanguageID</th><th>LanguageName</th><th>LanguageCode</th><th>Priority</th><th>RegDate</th><th>Status</th></tr></thead>
<tbody id="LanguageCode"></tbody>
</table>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.