0

I am trying to pull an error message from an external errors.txt file.

The contents of errors.txt is:

var data = {
'errors': [{
            "code": "9999",
            "message": {
                row: 1,
                col: 0,
                data: 'ABC',
                code: 20016,
                message: 'Invalid code.'
                }
             },
             {
              "code": "9999",
              "message": {
                  row: 1,
                  col: 0,
                  data: 'DEF!',
                  code: 20016,
                  volume: '100',
                  message: 'Invalid code.'
                  }
              }],
"data": {
    "report": null
},
"successMsg": null,
"errorMsg": null

}

I am trying to display the message (ie - "invalid code') and have tried the following, but no luck:

  jQuery.getJSON('/js/errors.txt', function (data) {
            // data is an array of objects
            $.each(data, function () {
                alert(this.errors[0].message[0].message);                
            });
        });

What am I doing wrong?

4
  • No error message, nothing in console. Commented Mar 18, 2016 at 5:48
  • Could you setup a plunkr or fiddle? Commented Mar 18, 2016 at 5:57
  • 1
    If you open errors.txt in the browser it probably displays text with "var data = " which is not a valid JSON. Try to remove "var data = " Commented Mar 18, 2016 at 6:11
  • @SatejS I did create a plunkr here but I think I'm not pointing to the errors.txt file correctly: plnkr.co/edit/v20HPmhMQmmYCzQcDCDf Commented Mar 18, 2016 at 6:28

2 Answers 2

2

Please try as below:

errors.txt

{
  "errors": [
    {
      "code": "9999",
      "message": {
        "row": 1,
        "col": 0,
        "data": "ABC",
        "code": 20016,
        "message": "Invalid code."
      }
    },
    {
      "code": "9999",
      "message": {
        "row": 1,
        "col": 0,
        "data": "DEF!",
        "code": 20016,
        "volume": 100,
        "message": "Invalid code."
      }
    }
  ],
  "data": {
    "report": null
  },
  "successMsg": null,
  "errorMsg": null
}  

script

<script>
    $(function () {
        $.getJSON('errors.txt', function (data) {
            $.each(data, function () {
                alert(data.errors[0].message.message);
            });
        });
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

When you are traversing your JSON with each loop try to use key & value(give your own name according to your coding choice) as parameter to it.

Please replace your inner $.each({ loop with below code

$.each(data, function (key, value) {
    alert("Error Message : ", value[0].message.message);
});

Hope this helps to tackle the problem..

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.