Error in getting data from ajax json response









up vote
0
down vote

favorite












I'm trying to get data from the JSON response from the ajax, when i try to get data outside of the loop for there is no problem(the console.log command worked), but when it's inside it gives error




TypeError: Cannot read property 'TOTALITEM' of undefined




the console.log command gave the error above.The same happened on the second for, on "orc[i].NITEM" but it worked i don't know why.
I also tried to sum the values and append in a second place but it also didn't work. i would be really gratefull if someone could explain why this is happening.thanks!!



var obj= JSON.parse(data);
var orc = obj.DADOS.ORCITENS[0].ITENSORC.LITEM;
var tamanho = orc.length;
var total=0;
$(".lista-orcamento").empty();
console.log(orc[0].TOTALITEM);
for(var x = 0; x <= tamanho; x++)

console.log(orc[x].TOTALITEM);
total+= parseFloat(orc[x].TOTALITEM);


console.log(total);

for(var i = 0; i <= tamanho; i++)

$(".lista-orcamento").append
('<div class="row produtos" >'+
'<div class="col-1">'+orc[i].NITEM+'</div>'+
'<div class="col-1"> <input type="text" class="form-control codigo" value='+orc[i].CODPRODUTO+'></div>'+
'<div class="col-1"> <input type="text" class="form-control quantidade" value='+orc[i].QTDVENDIDO+'></div>'+
'<div class="col-3"> <input type="text" class="form-control descricao" value='+orc[i].DESCRICAO+'></div>'+
'<div class="col-1">'+
'<select class="form-control prcu">'+
'<option value="001">'+orc[i].PRCUNIT+'</option>'+
'</select></div>'+
'<div class="col-1"> <input type="text" class="form-control prcv" value='+orc[i].PRCVENDA+'></div>'+
'<div class="col-1"> <input type="text" class="form-control desco" value='+orc[i].DESCPUNIT+'></div>'+
'<div class="col-1"> <input type="text" class="form-control prct" value='+orc[i].TOTALITEM+'></div>'+
'<div class="col-1"> <input type="text" class="form-control descot" value='+orc[i].DESCVUNIT+'></div>'+
'<div class="col-1"> <input type="text" class="form-control obs" value='+orc[i].OBSITEM+'></div>'+
'</div>'
);
// fim do for

}//fim sucesso









share|improve this question





















  • Please provide a Minimal, Complete, and Verifiable example: stackoverflow.com/help/mcve We need to see an example of the data or how you got the data.
    – Twisty
    Nov 9 at 3:22










  • Trace back what is undefined. Orc[0] is most likely undefined. Show what orc is to console. If it is undefined go back to the parts of what made it. Eventually you will find the problems source.
    – Mike Wodarczyk
    Nov 9 at 3:35










  • The json result is a complecated nested list .Here TOTALITEM may not the member of obj.DADOS.ORCITENS[0].ITENSORC.LITEM. IF you would like to discribe the structure of the converted list of json may provide you the suggestion
    – LDS
    Nov 9 at 5:22














up vote
0
down vote

favorite












I'm trying to get data from the JSON response from the ajax, when i try to get data outside of the loop for there is no problem(the console.log command worked), but when it's inside it gives error




TypeError: Cannot read property 'TOTALITEM' of undefined




the console.log command gave the error above.The same happened on the second for, on "orc[i].NITEM" but it worked i don't know why.
I also tried to sum the values and append in a second place but it also didn't work. i would be really gratefull if someone could explain why this is happening.thanks!!



var obj= JSON.parse(data);
var orc = obj.DADOS.ORCITENS[0].ITENSORC.LITEM;
var tamanho = orc.length;
var total=0;
$(".lista-orcamento").empty();
console.log(orc[0].TOTALITEM);
for(var x = 0; x <= tamanho; x++)

console.log(orc[x].TOTALITEM);
total+= parseFloat(orc[x].TOTALITEM);


console.log(total);

for(var i = 0; i <= tamanho; i++)

$(".lista-orcamento").append
('<div class="row produtos" >'+
'<div class="col-1">'+orc[i].NITEM+'</div>'+
'<div class="col-1"> <input type="text" class="form-control codigo" value='+orc[i].CODPRODUTO+'></div>'+
'<div class="col-1"> <input type="text" class="form-control quantidade" value='+orc[i].QTDVENDIDO+'></div>'+
'<div class="col-3"> <input type="text" class="form-control descricao" value='+orc[i].DESCRICAO+'></div>'+
'<div class="col-1">'+
'<select class="form-control prcu">'+
'<option value="001">'+orc[i].PRCUNIT+'</option>'+
'</select></div>'+
'<div class="col-1"> <input type="text" class="form-control prcv" value='+orc[i].PRCVENDA+'></div>'+
'<div class="col-1"> <input type="text" class="form-control desco" value='+orc[i].DESCPUNIT+'></div>'+
'<div class="col-1"> <input type="text" class="form-control prct" value='+orc[i].TOTALITEM+'></div>'+
'<div class="col-1"> <input type="text" class="form-control descot" value='+orc[i].DESCVUNIT+'></div>'+
'<div class="col-1"> <input type="text" class="form-control obs" value='+orc[i].OBSITEM+'></div>'+
'</div>'
);
// fim do for

}//fim sucesso









share|improve this question





















  • Please provide a Minimal, Complete, and Verifiable example: stackoverflow.com/help/mcve We need to see an example of the data or how you got the data.
    – Twisty
    Nov 9 at 3:22










  • Trace back what is undefined. Orc[0] is most likely undefined. Show what orc is to console. If it is undefined go back to the parts of what made it. Eventually you will find the problems source.
    – Mike Wodarczyk
    Nov 9 at 3:35










  • The json result is a complecated nested list .Here TOTALITEM may not the member of obj.DADOS.ORCITENS[0].ITENSORC.LITEM. IF you would like to discribe the structure of the converted list of json may provide you the suggestion
    – LDS
    Nov 9 at 5:22












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to get data from the JSON response from the ajax, when i try to get data outside of the loop for there is no problem(the console.log command worked), but when it's inside it gives error




TypeError: Cannot read property 'TOTALITEM' of undefined




the console.log command gave the error above.The same happened on the second for, on "orc[i].NITEM" but it worked i don't know why.
I also tried to sum the values and append in a second place but it also didn't work. i would be really gratefull if someone could explain why this is happening.thanks!!



var obj= JSON.parse(data);
var orc = obj.DADOS.ORCITENS[0].ITENSORC.LITEM;
var tamanho = orc.length;
var total=0;
$(".lista-orcamento").empty();
console.log(orc[0].TOTALITEM);
for(var x = 0; x <= tamanho; x++)

console.log(orc[x].TOTALITEM);
total+= parseFloat(orc[x].TOTALITEM);


console.log(total);

for(var i = 0; i <= tamanho; i++)

$(".lista-orcamento").append
('<div class="row produtos" >'+
'<div class="col-1">'+orc[i].NITEM+'</div>'+
'<div class="col-1"> <input type="text" class="form-control codigo" value='+orc[i].CODPRODUTO+'></div>'+
'<div class="col-1"> <input type="text" class="form-control quantidade" value='+orc[i].QTDVENDIDO+'></div>'+
'<div class="col-3"> <input type="text" class="form-control descricao" value='+orc[i].DESCRICAO+'></div>'+
'<div class="col-1">'+
'<select class="form-control prcu">'+
'<option value="001">'+orc[i].PRCUNIT+'</option>'+
'</select></div>'+
'<div class="col-1"> <input type="text" class="form-control prcv" value='+orc[i].PRCVENDA+'></div>'+
'<div class="col-1"> <input type="text" class="form-control desco" value='+orc[i].DESCPUNIT+'></div>'+
'<div class="col-1"> <input type="text" class="form-control prct" value='+orc[i].TOTALITEM+'></div>'+
'<div class="col-1"> <input type="text" class="form-control descot" value='+orc[i].DESCVUNIT+'></div>'+
'<div class="col-1"> <input type="text" class="form-control obs" value='+orc[i].OBSITEM+'></div>'+
'</div>'
);
// fim do for

}//fim sucesso









share|improve this question













I'm trying to get data from the JSON response from the ajax, when i try to get data outside of the loop for there is no problem(the console.log command worked), but when it's inside it gives error




TypeError: Cannot read property 'TOTALITEM' of undefined




the console.log command gave the error above.The same happened on the second for, on "orc[i].NITEM" but it worked i don't know why.
I also tried to sum the values and append in a second place but it also didn't work. i would be really gratefull if someone could explain why this is happening.thanks!!



var obj= JSON.parse(data);
var orc = obj.DADOS.ORCITENS[0].ITENSORC.LITEM;
var tamanho = orc.length;
var total=0;
$(".lista-orcamento").empty();
console.log(orc[0].TOTALITEM);
for(var x = 0; x <= tamanho; x++)

console.log(orc[x].TOTALITEM);
total+= parseFloat(orc[x].TOTALITEM);


console.log(total);

for(var i = 0; i <= tamanho; i++)

$(".lista-orcamento").append
('<div class="row produtos" >'+
'<div class="col-1">'+orc[i].NITEM+'</div>'+
'<div class="col-1"> <input type="text" class="form-control codigo" value='+orc[i].CODPRODUTO+'></div>'+
'<div class="col-1"> <input type="text" class="form-control quantidade" value='+orc[i].QTDVENDIDO+'></div>'+
'<div class="col-3"> <input type="text" class="form-control descricao" value='+orc[i].DESCRICAO+'></div>'+
'<div class="col-1">'+
'<select class="form-control prcu">'+
'<option value="001">'+orc[i].PRCUNIT+'</option>'+
'</select></div>'+
'<div class="col-1"> <input type="text" class="form-control prcv" value='+orc[i].PRCVENDA+'></div>'+
'<div class="col-1"> <input type="text" class="form-control desco" value='+orc[i].DESCPUNIT+'></div>'+
'<div class="col-1"> <input type="text" class="form-control prct" value='+orc[i].TOTALITEM+'></div>'+
'<div class="col-1"> <input type="text" class="form-control descot" value='+orc[i].DESCVUNIT+'></div>'+
'<div class="col-1"> <input type="text" class="form-control obs" value='+orc[i].OBSITEM+'></div>'+
'</div>'
);
// fim do for

}//fim sucesso






javascript jquery json ajax






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 2:45









blobblob

238




238











  • Please provide a Minimal, Complete, and Verifiable example: stackoverflow.com/help/mcve We need to see an example of the data or how you got the data.
    – Twisty
    Nov 9 at 3:22










  • Trace back what is undefined. Orc[0] is most likely undefined. Show what orc is to console. If it is undefined go back to the parts of what made it. Eventually you will find the problems source.
    – Mike Wodarczyk
    Nov 9 at 3:35










  • The json result is a complecated nested list .Here TOTALITEM may not the member of obj.DADOS.ORCITENS[0].ITENSORC.LITEM. IF you would like to discribe the structure of the converted list of json may provide you the suggestion
    – LDS
    Nov 9 at 5:22
















  • Please provide a Minimal, Complete, and Verifiable example: stackoverflow.com/help/mcve We need to see an example of the data or how you got the data.
    – Twisty
    Nov 9 at 3:22










  • Trace back what is undefined. Orc[0] is most likely undefined. Show what orc is to console. If it is undefined go back to the parts of what made it. Eventually you will find the problems source.
    – Mike Wodarczyk
    Nov 9 at 3:35










  • The json result is a complecated nested list .Here TOTALITEM may not the member of obj.DADOS.ORCITENS[0].ITENSORC.LITEM. IF you would like to discribe the structure of the converted list of json may provide you the suggestion
    – LDS
    Nov 9 at 5:22















Please provide a Minimal, Complete, and Verifiable example: stackoverflow.com/help/mcve We need to see an example of the data or how you got the data.
– Twisty
Nov 9 at 3:22




Please provide a Minimal, Complete, and Verifiable example: stackoverflow.com/help/mcve We need to see an example of the data or how you got the data.
– Twisty
Nov 9 at 3:22












Trace back what is undefined. Orc[0] is most likely undefined. Show what orc is to console. If it is undefined go back to the parts of what made it. Eventually you will find the problems source.
– Mike Wodarczyk
Nov 9 at 3:35




Trace back what is undefined. Orc[0] is most likely undefined. Show what orc is to console. If it is undefined go back to the parts of what made it. Eventually you will find the problems source.
– Mike Wodarczyk
Nov 9 at 3:35












The json result is a complecated nested list .Here TOTALITEM may not the member of obj.DADOS.ORCITENS[0].ITENSORC.LITEM. IF you would like to discribe the structure of the converted list of json may provide you the suggestion
– LDS
Nov 9 at 5:22




The json result is a complecated nested list .Here TOTALITEM may not the member of obj.DADOS.ORCITENS[0].ITENSORC.LITEM. IF you would like to discribe the structure of the converted list of json may provide you the suggestion
– LDS
Nov 9 at 5:22












1 Answer
1






active

oldest

votes

















up vote
0
down vote













If one assumes that the code you have supplied is within a success callback, it may look something like:



$.ajax(
url: "get-orc-data.php",
data: id: 0 ,
dataType: "json",
method: "post",
success: function(data)
// Your Code
var obj = JSON.parse(data);
var total = 0;

);


In the example above, remember that obj and total will be defined only for the scope of that function. You can not call obj or total outside of that function. If you need that variable to be updated, create one that is in scope for a more global arena.



Consider this code:



$(function()
var total = 0;
$.ajax(
url: "get-orc-data.php",
data: id: 0 ,
dataType: "json",
method: "post",
success: function(data)
console.log(data);
var orc = data.DADOS.ORCITENS[0].ITENSORC.LITEM;
$(".lista-orcamento").empty();
console.log("Counting Total: " + total);
$.each(orc, function(k, v)
console.log("Adding: " + v.TOTALITEM);
total += parseFloat(v.TOTALITEM);
);
console.log("Total Calculated: " + total);
$.each(orc, function(k, v)
$(".lista-orcamento").append(
'<div class="row produtos" >',
'<div class="col-1">' + v.NITEM + '</div>',
'<div class="col-1"> <input type="text" class="form-control codigo" value=' + v.CODPRODUTO + '></div>',
'<div class="col-1"> <input type="text" class="form-control quantidade" value=' + v.QTDVENDIDO + '></div>',
'<div class="col-3"> <input type="text" class="form-control descricao" value=' + v.DESCRICAO + '></div>',
'<div class="col-1">',
'<select class="form-control prcu">',
'<option value="001">' + v.PRCUNIT + '</option>',
'</select></div>',
'<div class="col-1"> <input type="text" class="form-control prcv" value=' + v.PRCVENDA + '></div>',
'<div class="col-1"> <input type="text" class="form-control desco" value=' + v.DESCPUNIT + '></div>',
'<div class="col-1"> <input type="text" class="form-control prct" value=' + v.TOTALITEM + '></div>',
'<div class="col-1"> <input type="text" class="form-control descot" value=' + v.DESCVUNIT + '></div>',
'<div class="col-1"> <input type="text" class="form-control obs" value=' + v.OBSITEM + '></div>'+
'</div>'
);
);

);
);


Now total can be used in other functions.



Also, since the $.ajax() call has the dataType defined, it should be expecting a JSON object already. Reviewing the console should confirm this as there should be an object returned with the data structure you're expecting. If you're getting something else, you will want to review the code and ensure it matches the data structure or review the API.



Hope that helps.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219126%2ferror-in-getting-data-from-ajax-json-response%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    If one assumes that the code you have supplied is within a success callback, it may look something like:



    $.ajax(
    url: "get-orc-data.php",
    data: id: 0 ,
    dataType: "json",
    method: "post",
    success: function(data)
    // Your Code
    var obj = JSON.parse(data);
    var total = 0;

    );


    In the example above, remember that obj and total will be defined only for the scope of that function. You can not call obj or total outside of that function. If you need that variable to be updated, create one that is in scope for a more global arena.



    Consider this code:



    $(function()
    var total = 0;
    $.ajax(
    url: "get-orc-data.php",
    data: id: 0 ,
    dataType: "json",
    method: "post",
    success: function(data)
    console.log(data);
    var orc = data.DADOS.ORCITENS[0].ITENSORC.LITEM;
    $(".lista-orcamento").empty();
    console.log("Counting Total: " + total);
    $.each(orc, function(k, v)
    console.log("Adding: " + v.TOTALITEM);
    total += parseFloat(v.TOTALITEM);
    );
    console.log("Total Calculated: " + total);
    $.each(orc, function(k, v)
    $(".lista-orcamento").append(
    '<div class="row produtos" >',
    '<div class="col-1">' + v.NITEM + '</div>',
    '<div class="col-1"> <input type="text" class="form-control codigo" value=' + v.CODPRODUTO + '></div>',
    '<div class="col-1"> <input type="text" class="form-control quantidade" value=' + v.QTDVENDIDO + '></div>',
    '<div class="col-3"> <input type="text" class="form-control descricao" value=' + v.DESCRICAO + '></div>',
    '<div class="col-1">',
    '<select class="form-control prcu">',
    '<option value="001">' + v.PRCUNIT + '</option>',
    '</select></div>',
    '<div class="col-1"> <input type="text" class="form-control prcv" value=' + v.PRCVENDA + '></div>',
    '<div class="col-1"> <input type="text" class="form-control desco" value=' + v.DESCPUNIT + '></div>',
    '<div class="col-1"> <input type="text" class="form-control prct" value=' + v.TOTALITEM + '></div>',
    '<div class="col-1"> <input type="text" class="form-control descot" value=' + v.DESCVUNIT + '></div>',
    '<div class="col-1"> <input type="text" class="form-control obs" value=' + v.OBSITEM + '></div>'+
    '</div>'
    );
    );

    );
    );


    Now total can be used in other functions.



    Also, since the $.ajax() call has the dataType defined, it should be expecting a JSON object already. Reviewing the console should confirm this as there should be an object returned with the data structure you're expecting. If you're getting something else, you will want to review the code and ensure it matches the data structure or review the API.



    Hope that helps.






    share|improve this answer
























      up vote
      0
      down vote













      If one assumes that the code you have supplied is within a success callback, it may look something like:



      $.ajax(
      url: "get-orc-data.php",
      data: id: 0 ,
      dataType: "json",
      method: "post",
      success: function(data)
      // Your Code
      var obj = JSON.parse(data);
      var total = 0;

      );


      In the example above, remember that obj and total will be defined only for the scope of that function. You can not call obj or total outside of that function. If you need that variable to be updated, create one that is in scope for a more global arena.



      Consider this code:



      $(function()
      var total = 0;
      $.ajax(
      url: "get-orc-data.php",
      data: id: 0 ,
      dataType: "json",
      method: "post",
      success: function(data)
      console.log(data);
      var orc = data.DADOS.ORCITENS[0].ITENSORC.LITEM;
      $(".lista-orcamento").empty();
      console.log("Counting Total: " + total);
      $.each(orc, function(k, v)
      console.log("Adding: " + v.TOTALITEM);
      total += parseFloat(v.TOTALITEM);
      );
      console.log("Total Calculated: " + total);
      $.each(orc, function(k, v)
      $(".lista-orcamento").append(
      '<div class="row produtos" >',
      '<div class="col-1">' + v.NITEM + '</div>',
      '<div class="col-1"> <input type="text" class="form-control codigo" value=' + v.CODPRODUTO + '></div>',
      '<div class="col-1"> <input type="text" class="form-control quantidade" value=' + v.QTDVENDIDO + '></div>',
      '<div class="col-3"> <input type="text" class="form-control descricao" value=' + v.DESCRICAO + '></div>',
      '<div class="col-1">',
      '<select class="form-control prcu">',
      '<option value="001">' + v.PRCUNIT + '</option>',
      '</select></div>',
      '<div class="col-1"> <input type="text" class="form-control prcv" value=' + v.PRCVENDA + '></div>',
      '<div class="col-1"> <input type="text" class="form-control desco" value=' + v.DESCPUNIT + '></div>',
      '<div class="col-1"> <input type="text" class="form-control prct" value=' + v.TOTALITEM + '></div>',
      '<div class="col-1"> <input type="text" class="form-control descot" value=' + v.DESCVUNIT + '></div>',
      '<div class="col-1"> <input type="text" class="form-control obs" value=' + v.OBSITEM + '></div>'+
      '</div>'
      );
      );

      );
      );


      Now total can be used in other functions.



      Also, since the $.ajax() call has the dataType defined, it should be expecting a JSON object already. Reviewing the console should confirm this as there should be an object returned with the data structure you're expecting. If you're getting something else, you will want to review the code and ensure it matches the data structure or review the API.



      Hope that helps.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        If one assumes that the code you have supplied is within a success callback, it may look something like:



        $.ajax(
        url: "get-orc-data.php",
        data: id: 0 ,
        dataType: "json",
        method: "post",
        success: function(data)
        // Your Code
        var obj = JSON.parse(data);
        var total = 0;

        );


        In the example above, remember that obj and total will be defined only for the scope of that function. You can not call obj or total outside of that function. If you need that variable to be updated, create one that is in scope for a more global arena.



        Consider this code:



        $(function()
        var total = 0;
        $.ajax(
        url: "get-orc-data.php",
        data: id: 0 ,
        dataType: "json",
        method: "post",
        success: function(data)
        console.log(data);
        var orc = data.DADOS.ORCITENS[0].ITENSORC.LITEM;
        $(".lista-orcamento").empty();
        console.log("Counting Total: " + total);
        $.each(orc, function(k, v)
        console.log("Adding: " + v.TOTALITEM);
        total += parseFloat(v.TOTALITEM);
        );
        console.log("Total Calculated: " + total);
        $.each(orc, function(k, v)
        $(".lista-orcamento").append(
        '<div class="row produtos" >',
        '<div class="col-1">' + v.NITEM + '</div>',
        '<div class="col-1"> <input type="text" class="form-control codigo" value=' + v.CODPRODUTO + '></div>',
        '<div class="col-1"> <input type="text" class="form-control quantidade" value=' + v.QTDVENDIDO + '></div>',
        '<div class="col-3"> <input type="text" class="form-control descricao" value=' + v.DESCRICAO + '></div>',
        '<div class="col-1">',
        '<select class="form-control prcu">',
        '<option value="001">' + v.PRCUNIT + '</option>',
        '</select></div>',
        '<div class="col-1"> <input type="text" class="form-control prcv" value=' + v.PRCVENDA + '></div>',
        '<div class="col-1"> <input type="text" class="form-control desco" value=' + v.DESCPUNIT + '></div>',
        '<div class="col-1"> <input type="text" class="form-control prct" value=' + v.TOTALITEM + '></div>',
        '<div class="col-1"> <input type="text" class="form-control descot" value=' + v.DESCVUNIT + '></div>',
        '<div class="col-1"> <input type="text" class="form-control obs" value=' + v.OBSITEM + '></div>'+
        '</div>'
        );
        );

        );
        );


        Now total can be used in other functions.



        Also, since the $.ajax() call has the dataType defined, it should be expecting a JSON object already. Reviewing the console should confirm this as there should be an object returned with the data structure you're expecting. If you're getting something else, you will want to review the code and ensure it matches the data structure or review the API.



        Hope that helps.






        share|improve this answer












        If one assumes that the code you have supplied is within a success callback, it may look something like:



        $.ajax(
        url: "get-orc-data.php",
        data: id: 0 ,
        dataType: "json",
        method: "post",
        success: function(data)
        // Your Code
        var obj = JSON.parse(data);
        var total = 0;

        );


        In the example above, remember that obj and total will be defined only for the scope of that function. You can not call obj or total outside of that function. If you need that variable to be updated, create one that is in scope for a more global arena.



        Consider this code:



        $(function()
        var total = 0;
        $.ajax(
        url: "get-orc-data.php",
        data: id: 0 ,
        dataType: "json",
        method: "post",
        success: function(data)
        console.log(data);
        var orc = data.DADOS.ORCITENS[0].ITENSORC.LITEM;
        $(".lista-orcamento").empty();
        console.log("Counting Total: " + total);
        $.each(orc, function(k, v)
        console.log("Adding: " + v.TOTALITEM);
        total += parseFloat(v.TOTALITEM);
        );
        console.log("Total Calculated: " + total);
        $.each(orc, function(k, v)
        $(".lista-orcamento").append(
        '<div class="row produtos" >',
        '<div class="col-1">' + v.NITEM + '</div>',
        '<div class="col-1"> <input type="text" class="form-control codigo" value=' + v.CODPRODUTO + '></div>',
        '<div class="col-1"> <input type="text" class="form-control quantidade" value=' + v.QTDVENDIDO + '></div>',
        '<div class="col-3"> <input type="text" class="form-control descricao" value=' + v.DESCRICAO + '></div>',
        '<div class="col-1">',
        '<select class="form-control prcu">',
        '<option value="001">' + v.PRCUNIT + '</option>',
        '</select></div>',
        '<div class="col-1"> <input type="text" class="form-control prcv" value=' + v.PRCVENDA + '></div>',
        '<div class="col-1"> <input type="text" class="form-control desco" value=' + v.DESCPUNIT + '></div>',
        '<div class="col-1"> <input type="text" class="form-control prct" value=' + v.TOTALITEM + '></div>',
        '<div class="col-1"> <input type="text" class="form-control descot" value=' + v.DESCVUNIT + '></div>',
        '<div class="col-1"> <input type="text" class="form-control obs" value=' + v.OBSITEM + '></div>'+
        '</div>'
        );
        );

        );
        );


        Now total can be used in other functions.



        Also, since the $.ajax() call has the dataType defined, it should be expecting a JSON object already. Reviewing the console should confirm this as there should be an object returned with the data structure you're expecting. If you're getting something else, you will want to review the code and ensure it matches the data structure or review the API.



        Hope that helps.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 7:37









        Twisty

        12.6k11433




        12.6k11433



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219126%2ferror-in-getting-data-from-ajax-json-response%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Edmonton

            Crossroads (UK TV series)