Author Topic: How can I display an error message when JSON call fails?  (Read 3370 times)

Eagle_f90

  • Newbie
  • *
  • Posts: 15
    • View Profile
How can I display an error message when JSON call fails?
« on: February 06, 2016, 03:11:59 am »
I am able to successfully display data in my grid when I don't run into any problems getting the data but now I want to display an error message to the user if I run into any problems getting the data. I found http://paramquery.com/api#option-dataModel-error but am not sure how to combine it with a successful call. Also, my understanding of the 3 parameters in that function are that they will only return the core Error data and not a custom message that I return. I can display the Error status code if I have to but it would be nice to display a more user friendly message if possible. Thank you

Code: [Select]
var dataModel =
            {
                location: "remote",
                dataType: "json",
                method: "POST",
                contentType: "application/json; charset=UTF-8",
                url: '@Url.RouteUrl("GetChangeOrders")',
                getData: function (dataJson)
                {
                    return { data: dataJson };
                }
            };

            var obj =
            {
                collapsible: false,
                colModel: colModel,
                dataModel: dataModel,
                editable: false,
                filterModel: { on: true, mode: "AND", header: true },
                flexHeight: true,
                numberCell: { show: false },
                roundCorners: true,
                scrollModel: [lastColumn = "None"],
                sortDir: ["up"],
                sortIndx: ["ID"],
                sorting: "local",
                stripeRows: true,
                title: "IT Change Orders",
                width: $(window).width() - 10,
                wrap: false
            };

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: How can I display an error message when JSON call fails?
« Reply #1 on: February 08, 2016, 10:52:32 am »
Displaying a custom error message is simple:

dataModel:{
  error: function( ){
    alert( 'custom message' );
  }
  ,..
}

Eagle_f90

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: How can I display an error message when JSON call fails?
« Reply #2 on: February 09, 2016, 02:12:22 am »
But that would do a pop-up error correct? I don't want a popup for an error displayed like the "no rows" message.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: How can I display an error message when JSON call fails?
« Reply #3 on: February 09, 2016, 09:17:00 am »
That one is:

Code: [Select]
error: function( ){

var oldMsg = $grid.pqGrid( "option", "strNoRows" );
$grid.pqGrid( "option", "strNoRows", "custom message");
$grid.pqGrid( "refresh" );

//restore the normal message.
$grid.pqGrid( "option", "strNoRows", oldMsg);
},
« Last Edit: February 09, 2016, 09:27:41 am by paramquery »

Eagle_f90

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: How can I display an error message when JSON call fails?
« Reply #4 on: February 09, 2016, 07:35:04 pm »
Thanks for the help