This is a migrated thread and some comments may be shown as answers.

[Solved] How to handle the custom server response correctly after updating.

5 Answers 403 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Den
Top achievements
Rank 1
Den asked on 17 Dec 2014, 07:36 PM
How to handle the custom server response correctly after updating?

I have the one strange problem.
Server returns List<MyDto> and this information are displayed in the grid.

class MyDto{
    String name;
    Guid id;
    String userName;
    Guid userId;
}

Grid has two columns "name" and "user name".
Where the first column is just noneditable cell and the second column is dropdownlist cell, where I can change an user.
Grid has an "inline" editable mode.

My update js code looks like
update: {
    url: "api/linkUser",
    method: 'POST',
    complete: function(e) {
        refreshGrid();
    }
},
parameterMap: function (options, operation) {
 
    if (operation !== "read" && options.models) {
        var data = {};
        data.Id = JSON.parse(JSON.stringify(options.models))[0].Id;
        data.UserId = JSON.parse(JSON.stringify(options.models))[0].UserId;
 
        return data;
    }
},

Server method:
public Result linkUser(Guid id, Guid userId)
{
    bool result = CheckAndSave(id, userId);
    if(result)
    {
        return new Result(true, "Success!");
    }
    else
    {
        return new Result(false, "Error!");
    }
}
So I 'd like to handle result on client side and show notification if I get an error.

But on client-side I get "​Uncaught SyntaxError: Unexpected token u" and update-complete function does not called.

What's problem? How to supress this error? May be there is a workaround?

Thanks in advance.

5 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 19 Dec 2014, 01:49 PM
Hello Den,

The errors raised during communication with the server can be handled in the error event of the DataSource. This error event will be raised if either the request fails, or if the call is successful but the response contains a special 'errors' field. The later is designed for the custom errors scenarios in mind. Thus, the server should return the error in a field which name should be configured via the schema->errors configuration.

In your particular scenario, you should for example modify the Result to have a Errors field to which the error text can be assign. Then you can handle the error event and display some kind of notification to the user. For example:

new kendo.data.DataSource({
  transport: {
     /*..*/
  },
  schema: {       
    errors: "Errors"
  },
  error: function(e) {
    if (e.errors) {
      alert(e.errors);
    }
  },
  /*..*/
});


Regards,
Rosen
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Den
Top achievements
Rank 1
answered on 13 Jan 2015, 02:10 PM
Hello Rosen.

Thanks for reply, I just come back to this project and I used the your answer.
But it's not work.

My Result class
public class Result {
 
    private boolean error;
 
    private String result;
 
    public Result(boolean error, String result) {
        this.error = error;
        this.result = result;
    }
}

And I modified my js code
$("#grid").kendoGrid({
    dataSource: {
       transport: {
            .....
       },
       batch: true,
       error: function(e) {
            debugger;
            console.log(e.errors)
        },
        schema: {
            model: {
                id: "id",
                errors: "result",
                fields: {
                ...
                }
                ...
            }
        }
    }
});
$('#grid').data('kendoGrid').dataSource.fetch();

But I get the same issue "​Uncaught SyntaxError: Unexpected token u".
What did I do wrong?
0
Rosen
Telerik team
answered on 14 Jan 2015, 09:55 AM

Hello Den,

Could you please clarify which server technology you are using? Also you should make sure the server method is returning valid JSON object with a field named result which to contain the error text. You could examine the server response by using the Chrome developer tools network tab.

Regards,
Rosen
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Den
Top achievements
Rank 1
answered on 14 Jan 2015, 02:00 PM
Hello Rosen,

My server is Java (Tomcat) and client is windows browser (for example Chrome).
It seems my server method is returning valid JSON object I get result in parse function
parse: function(response) {
                        console.log(JSON.stringify(info, null, 4));
 
                        debugger;
                        return response;
                    }
In console I see:
error: false
result: null
Then I press F8 (in Chrome developer) and I get "​Uncaught SyntaxError: Unexpected token u" issue.
So that update-complete function isn't called.

How you can see I set the debugger code but I can't reach error function.
0
Rosen
Telerik team
answered on 15 Jan 2015, 10:29 AM

Hello Den,

If this is the response from the server it would not work as the result field is empty - there are no errors. This is why the error event is not triggered.

I suspect that the fields are not serialized as they are not public but are marked as private.

If you continue to experience difficulties please provide a small runnable sample in which the issue can be observed locally.

Regards,
Rosen
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
Den
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Den
Top achievements
Rank 1
Share this question
or