0
var tdValue = $.parseJSON(getMedicationOrderInstance(freqKey, $.datepicker.formatDate('dd M yy', dateFrom), $.datepicker.formatDate('dd M yy', dateTo)));

this is my ajax call which is returning following json and I am trying to parse but throwing exception

Exception thrown and not caught json2.js (503,13)

 {
      "25": [
        "00:00",
        "05:00",
        "10:00",
        "15:00",
        "20:00"
      ],
      "26": [
        "01:00",
        "06:00",
        "11:00",
        "16:00",
        "21:00"
      ],
      "27": [
        "02:00",
        "07:00",
        "12:00",
        "17:00",
        "22:00"
      ]
    }

Guys please help. It's cracking my head.

function getMedicationOrderInstance(key, dateFrom, dateTo) {
    return $.when(
            $.ajax({
                url: 'ajax',
                dataType: 'json',
                data: {
                    cls: ".....MedicationSearchController",
                    mtd: "getFreqDates",
                    ses: SessID,
                    frequencyKey: key,
                    startDate: dateFrom,
                    endDate:dateTo

                }

            })
    ).then(function(data){
        if (data && data.success === true) {

            return data.results;
        } else {
            // alert(data.exception);
        }
    });
}
10
  • 1
    getMedicationOrderInstance seems async to me.. Commented Nov 25, 2016 at 5:57
  • your json seems valid Commented Nov 25, 2016 at 5:58
  • @Rayon getMedicationOrderInstance() is a function for ajax call. Commented Nov 25, 2016 at 6:05
  • @ray — Well in that case, play with the callback or promise Commented Nov 25, 2016 at 6:07
  • var tdValue = {}; getMedicationOrderInstance(freqKey, $.datepicker.formatDate('dd M yy', dateFrom), $.datepicker.formatDate('dd M yy', dateTo)).then(function(data){ tdValue = $.parseJSON(data)}); Commented Nov 25, 2016 at 6:09

1 Answer 1

1

First of all, you don't need to use $.parseJSON. $.ajax will do it for you, as long as you set dataType: 'json'.

If json is specified, the response is parsed using jQuery.parseJSON before being passed, as an object, to the success handler. The parsed JSON object is made available through the responseJSON property of the jqXHR object.

You should be able to keep $.when in getMedicationOrderInstance, but here I simplify it a little bit, and return the original jqXHR object. And let the consumer decide what to do with it.

function getMedicationOrderInstance(key, dateFrom, dateTo) {
    // Return the jqXHR here...
    return $.ajax({
                url: 'ajax',
                dataType: 'json',
                data: {
                    cls: ".....MedicationSearchController",
                    mtd: "getFreqDates",
                    ses: SessID,
                    frequencyKey: key,
                    startDate: dateFrom,
                    endDate:dateTo

                }
            });
}

Then you can do like this:

var tdValue = {};
// I moved these to separate variables to increase readability only...
var parsedDateFrom = $.datepicker.formatDate('dd M yy', dateFrom);
var parsedDateTo = $.datepicker.formatDate('dd M yy', dateTo);

var defObj = getMedicationOrderInstance(freqKey, parsedDateFrom, parsedDateTo);

// When the call is complete do this.
defObj.then(function(data){
    // If you properly set dataType in the ajax-call you don't need to parse json.
    console.log(data.result);
    tdValue = data.result;

    // or possibly...
    // tdValue = data; 

    alert('done');
});
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.