UZUMAKI
UZUMAKI

Reputation: 43

Jqgrid show errors from json

My problem is this, I want to show errors through the information I get the json. That is, when we added / edited in jgrid something we always expect to get a response in json data. Then I remembered what it was, if you can not add something, I send in the json error.

I wanted to show the error using the Highlight / Error in Jquery. And therein lies my problem, how do I show it. From what I've been searching, should be the attribute aftersubmit implement and handle the response here but just that I am not getting.

Code:

afterSubmit: function (response) 
{
   if(responce == false)
        alert("test");
}

Format error in json:

{"Response":false,"Message":"Dados inválidos"}

Someone can help me?

---------------------------------------- SOLUTION ------------------------------------------------------

jQuery.extend(jQuery.jgrid.edit, 
    {
        reloadAfterSubmit:false,
        afterSubmit: function (response, postdata) 
        {
            if(jQuery.parseJSON(response.responseText).Response  == false)
            {
                alert(jQuery.parseJSON(response.responseText).Message);
                return [false,null,null];
            }
            else
            {
                return [true,null,null];
            }
        }
    }); 

Upvotes: 1

Views: 931

Answers (2)

Kris
Kris

Reputation: 1902

Try this

afterSubmit: function (response, postdata) 
{

  if(jQuery.parseJSON(response.responseText).Response)
alert(jQuery.parseJSON(response.responseText).Message);

}

Upvotes: 1

jeffery_the_wind
jeffery_the_wind

Reputation: 18188

I think for the error, just return the error as a string from the AJAX script. The JqGrid is expecting to get JSON data, but when it receives a string, it will display the error for you.

I use JqGrid with XML instead of JSON, but in my AJAX script, when I have an error, I just return a string instead of the XML that jqgrid is expecting, and the grid displays the error message correctly.

Please try this afterSubmit code and see if you see anything:

afterSubmit: function (response) 
{
   alert(response);
}

If you can see that, then you are all set, you just need to tweak the function to only display the response when there is actually an error.

Upvotes: 0

Related Questions