locked
How to receive a single string from ajax call RRS feed

  • Question

  • User1868852945 posted

    How can I get a simple, single string from an ajax call?  Normally I have been having the Controller return a result set of items via a Model like this...

    var parmObject = JSON.stringify({
    'p_db_type': db_type,
    'p_sch': sch,
    'p_own': own,
    'p_env': env,
    'p_baseline': baseline,
    'p_view_type': view_type,
    'p_last_days': last_days,
    'p_refresh_job_type': meta_refresh_type,
    });;
    $.ajax({
    url: "/SchemaTree/get_average_wait_time?p_db_type=" + db_type + "&p_sch=" + sch + "&p_own=" + own + "&p_env=" + env + "&p_baseline=" + baseline + "&p_view_type=" + view_type,
    type: 'POST',
    beforeSend: function () {
    $("#please_wait_box").show();
    },
    async: true,
    dataType: 'json',
    contentType: 'application/json',
    data: parmObject,
    success: function (data) {
    $.each(data, function (i, item) {
      //do stuff with the item...
    })
    },
    error: function (result) {
    alert("AJAX (st) Error: " + result.error);
    }
    });

    but when I have the Controller return a single string, the above success function gets syntax error...

    JavaScript runtime error: Invalid operand to 'in': Object expected

    Thanks much if you can advise.

    Wednesday, April 18, 2018 2:39 PM

Answers

  • User753101303 posted

    Hi,

    In  short data is now a string rather than an array so $;each doesn't make sense any more. What if you try just console.log(data); instead ? Do you see then the expected string when using F12 Console ?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 18, 2018 2:48 PM