How to process the response from server before the data will be serialized to Model instance?

  1. #1

    How to process the response from server before the data will be serialized to Model instance?

    Hi guys,

    I use Store -> RestProxy -> JsonReader. How can I process the raw data from server before that the data will pass to JsonReader?

    Thanks in advance!
  2. #2
    Hello @demos76, and welcome to Ext.NET Forums!

    It depends how you are doing the client-server communication. In general terms, I believe you want a callback handling the response, maybe success and failure ones.

    Hope this helps!
    Fabrício Murta
    Developer & Support Expert
  3. #3
    Hi @fabricio.murta

    I use code something like this:
     var treePanel1 =
                    new TreePanel
                    {
                        ID = "treeId",
                        Border = false,
                        RootVisible = false,
                        Layout = LayoutType.Fit.ToString(),
                        Header = false,
                        Store =
                        {
                            new TreeStore
                            {
                                ID = "storeId",
                                ShowWarningOnFailure = true,
                                AutoLoad = true,
                                Proxy =
                                        {
                                            new RestProxy
                                            {
                                                Url = "/{0}/{1}".FormatWith("controllerName", "readActionName"),
                                                ActionMethods =
                                                                        {
                                                                            Read = HttpMethod.POST,
                                                                            Create = HttpMethod.POST,
                                                                            Update = HttpMethod.POST,
                                                                            Destroy = HttpMethod.DELETE
                                                                        },
    
                                                API =
                                                                        {
                                                                            Read = "/{0}/{1}".FormatWith("controllerName", "readActionName"),
                                                                            Create = "/{0}/{1}".FormatWith("controllerName", "createActionName"),
                                                                            Update = "/{0}/{1}".FormatWith("controllerName", "updateActionName"),
                                                                            Destroy = "/{0}/{1}".FormatWith("controllerName", "destroyActionName")
                                                                        },
                                                Reader =
                                                                        {
                                                                            new JsonReader()
                                                                        },
                                                Writer =
                                                                        {
                                                                            new JsonWriter
                                                                                {
                                                                                    RootProperty = "data",
                                                                                    AllowSingle = true,
                                                                                    ClientIdProperty = "ID",
                                                                                    WriteAllFields = true,
                                                                                    Encode = true
                                                                                }
                                                                        }
                                            }
                                        }
                            }
                        },
                    };
    This is MVC application. And I want to correct the raw response (string containing JSON) from server before it will be deserialized to Model. Yes I want callback handling for success response. But I was not able to find appropriate listeners or events or methods in JsonProxy and JsonReader classes.

    Could you please provide an example or link to example with response processing?


    Thanks!
  4. #4
    Hello again, @demos76!

    If you want to change the response Ext.NET receives, before processing, your best bet is to actually change the endpoint you query from (that "/{0}/{1}".FormatWith("controllerName", "readActionName"), endpoint dynamically built depending on the address you open). That is, fix the response itself to fit the correct json format or provide custom data. It could come in the form of a new "wrapper" endpoint to that end.

    If you need to manipulate the response after it's received from the server but before Ext.NET consumes it, you could use the Ext.data.reader.Json.transform config to the JsonReader() property set as your tree's Reader and implement a client-side (javascript) handler to manipulate the data.

    And if you just need to points the root property where to look for the actual data from a json response, you can provide its path via the rootProperty config to the JsonReader definition, just like your JsonWriter does.

    I'm afraid we don't have any ready sample using this feature to share with you. But perhaps you have better luck reducing your problem to a simple test case to focus on the problem; then once you get a hang of the transform (if it really fits your scenario), implement the concept in the final project.

    A good starting point perhaps would be with a GridPanel using remote json data, just to understand the concept. Besides, with a simplified test case you could share a working sample where we could provide help with. Here's a couple simple examples that could be useful for this:
    - GridPanel > Plugins > GridFilters_Remote
    - GridPanel > Update > Restful

    The concept of a grid panel is very similar to a tree panel with server data. The first TreePanel in the following example uses server data (just not explicitly json-encapsulated), but it could serve as a good base if you want to make a simplified Json-fed TreePanel similar to the one you shared some code of.

    Hope this helps!
    Fabrício Murta
    Developer & Support Expert
  5. #5
    Hi @fabricio.murta

    Thanks a lot for your assistance. I will investigate your clues and report about results.

    Actually my problem is the free hosting :) It adds additional data to response dynamically (like adverts as script). So the response (JSON) is not valid in this case. And I can not fix it on the controller side.

    I just want to remove the extra data from response string before pass it to reader.
    Last edited by demos76; May 16, 2023 at 8:23 PM.
  6. #6
    Maybe you can have the hosting provide a valid response by ensuring the headers state the response type is application/json.

    Ext.NET should already do this but, for instance, say you have a .png file in the hosting, they know its type and don't prepend/append HTML or JavaScript code to the response, if for some reason the response doesn't state it is a JSON, the hosting may think it's plain HTML and mangle with the invalid data.

    The request headers, if you check using F12 developer tools in chrome (or similar in other browsers) should have something like:

    content-type: application/json; charset=utf-8
    So maybe all that you need is to properly prepare your response and let the (free) webserver know the response data is a JSON payload. Otherwise, maybe even the transform approach won't help, and you'll need to basically write your own JsonReader extension to handle the scenario, which would probably just break once you move the website to another hosting that doesn't mangle the response.

    Hope this helps!
    Fabrício Murta
    Developer & Support Expert
  7. #7
    Hi @fabricio.murta


    I've checked the response headers and there is wrong content type header:
    Click image for larger version. 

Name:	Screenshot 2023-05-18 220655.png 
Views:	41 
Size:	10.5 KB 
ID:	25613

    I will try to set application/json type a bit late. Unfortunately don't have free time for now:)

    Thanks for your advice.
  8. #8
    Hi @fabricio.murta


    Some actions in my controllers returned Json as string, so it leaded to that behavior. I've changed the string results to the StoreResult and currently it works well.


    Thanks a lot!

    The thread can be closed.

Similar Threads

  1. Replies: 0
    Last Post: Oct 14, 2013, 2:35 PM
  2. [1.0] Process POST result Json data
    By Maarten Boone in forum 1.x Help
    Replies: 0
    Last Post: Oct 31, 2010, 6:53 AM
  3. [CLOSED] [1.0] Accessing instance of store defined in custom server control
    By bryantharpe in forum 1.x Legacy Premium Help
    Replies: 8
    Last Post: Jun 28, 2010, 6:29 PM
  4. Mask to handle large server side process
    By NewGuy_001 in forum 1.x Help
    Replies: 2
    Last Post: Jun 12, 2010, 8:55 PM
  5. [CLOSED] CheckboxSelection model and script in every response
    By tdracz in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Apr 04, 2009, 11:09 AM

Tags for this Thread

Posting Permissions