Get JSON object from AJAX call

前端 未结 7 1514
难免孤独
难免孤独 2021-01-17 12:00

I\'m new to AJAX and javascript. In my project, I have to get a json object in my javascript file. I\'ve used spray

7条回答
  •  离开以前
    2021-01-17 12:20

    just console.log(data) you will see your object.

    you can access your value by something like this

    data.id //will give you id
    

    it also depend on you json how you are creating check this out for explanation

    // if it simply json then access it directly
    //Example => {"id":1,"value":"APPLE"}
    data.id; // will give you 1 
    
    // if it json array then you need to iterate over array and then get value.
    //Example => [{"id":1,"value":"APPLE"},{"id":2,"value":"MANGO"}] then
    data[0].id;  // will give you 1 
    

    so your code will be like this

     $.ajax({
        url: 'http://localhost:8081/all-modules',
        dataType: 'application/json',
        complete: function(data){
            alert(data.status);// S1000
            alert(data.description);// Success
            // for results you have to iterate because it is an array
            var len =  data.results.length;
            for(var i=0;i

提交回复
热议问题