locked
Empty Response Msg RRS feed

  • Question

  • User-1641868886 posted

    I have a success function of an ajax:get function: when user inputs a "Code" the response fills a label "Description" with the description info corresponding to the entered code.

    My issue is looking for an "empty" response ("Description") string, in order to have the response label state "Code not found" for the user.

    $.ajax({
                    type: "Get",
                    url: "/GrocBills/GetCodeDetails",
                    data: { Code: CurrCode },
                    dataType: "json",
                    
                    success: function (response) {
                        if (isEmpty(response)) {
                            $("#Code_Validation_Msg").html("Code not found");
                        }
                        else {
                            $("#Description").html(response);
                            $("#Code_Validation_Msg").hide();                        
                        }
                        
                    }
                    });
    
    ***I've tried several iterations of the "if" to check for empty or null response, like  "(response == 0), (response = null), (response.length === 0), etc.

    If you can point to what I'm missing, plz advise. I've seen several threads here and elsewhere to try to learn/revise, but no luck yet.

    Thanks much,

    RC

    Tuesday, February 18, 2020 6:12 PM

Answers

  • User281315223 posted

    What does your endpoint look like? If you are expecting a JSON payload coming back, it's very likely that your actual content is stored within a 'd' object under your response (e.g. response.d).

    I'd recommend using the Developer Tools (F12) within your browser and throwing a debugger statement in there to take a peek at what is coming back (or merely the Network traffic for this AJAX call) and then you can adjust your code accordingly:

    success: function(response){
         debugger;
         // Omitted for brevity (take a look at the response here)
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 18, 2020 7:56 PM

All replies

  • User281315223 posted

    What does your endpoint look like? If you are expecting a JSON payload coming back, it's very likely that your actual content is stored within a 'd' object under your response (e.g. response.d).

    I'd recommend using the Developer Tools (F12) within your browser and throwing a debugger statement in there to take a peek at what is coming back (or merely the Network traffic for this AJAX call) and then you can adjust your code accordingly:

    success: function(response){
         debugger;
         // Omitted for brevity (take a look at the response here)
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 18, 2020 7:56 PM
  • User-1641868886 posted

    Thanks, I was debugging and revising at the same time, but I wasn't getting anywhere, so I posted. I might should have waited, because I was just starting to revise to create an error function, which solved the issue:

                $.ajax({
                    type: "Get",
                    url: "/GrocBills/GetCodeDetails",
                    data: { Code: CurrCode },
                    dataType: "json",                
                    success: function (response) {
                            $("#Description").html(response);
                            $("#Code_Validation_Msg").hide();
                    },
                    error: function () {
                        $("#Description").empty();
                        $("#Code_Validation_Msg").html("Code does not exist");
                    }
                    
                 });

    The next <div> on the view is a button to "Create Code" if the above function advises the code the user inputs does not exist.

    Thanks for help.

    RC

    Tuesday, February 18, 2020 11:07 PM