im a beginer, i have a problem with my code, i cant pick up the specific data from my json with ajax and show it in my input text.

if i do $("#gakubuText").val(data.gakubu); doesnt work but if i do $("#gakubuText").val(data); it`s work

$.ajax({
        url:'http://localhost:8080/mvcsi/public/gakubu/getUpdate',
        data:{id:id},
        method:'post',
        type:'json',
        success: function(data){
            console.log(data);

//problem-> $("#gakubuText").val(data.gakubu);

        }

    });

this is my input text in modal box

<input type="text" class="form-control" id="gakubuText" value=""  placeholder="学部名" required>

this is the data from my console

{"code_gakubu":"8","gakubu":"AAA"}

1 Answers

0
Alonad On Best Solutions

It looks like you need Javascript object, but from server you get JSON. Try to use

JSON.parse(data)

This will transform JSON into plain object that you can access as data.gakubu