#1 2019-09-27 12:44:11

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Probably a simple problem, how should I return json?

Hi everyone,

I am building a mvc appilcation and try to use synopse-login with it for ajax calls. It's fine and working actually there isn't any real problem. I just need to clarify how should i use ınterface-based method's.

    $(document).ready(function(){
        $("button").click(function(){
            $.ajax({
                type: 'GET',
                url: "/root/DefaultMethods/test_List", 
                dataType: 'json', 
                contentType : 'application/json',            
                success: function(result){
                            console.log(result);

                            var json = $.parseJSON(result);
                            alert(json);


                            $("#div1").html(json);
                        },
                error: function(e){
                            alert(e);
                        }
                
            });
        });
    });

This is my simple ajax call for returning a JSON and my method looks like:

    Result := Server.RetrieveListJSON(TSQLTestRec, '', [], '', True);

And Here is my result:

'[{"RowID":1,"Employee":2,"Employer":2,"Title":"","StartMonth":8,"StartYear":2019,"CurrentlyWorking":true,"DepartureMonth":0,"DepartureYear":0,"ReasonForLeaving":""},{"RowID":2,"Employee":4,"Employer":2,"Title":"","StartMonth":8,"StartYear":2019,"CurrentlyWorking":true,"DepartureMonth":0,"DepartureYear":0,"ReasonForLeaving":""},{"RowID":17,"Employee":22,"Employer":2,"Title":"","StartMonth":9,"StartYear":2019,"CurrentlyWorking":true,"DepartureMonth":0,"DepartureYear":0,"ReasonForLeaving":""}]'#$A

I can see my result within console.log but my problem is, when i use paseJSON with result JQuery returns a parse error:

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data


How should i return JSON from my methods to avoid this?

Thanks for helps

Best regards.

Offline

#2 2019-09-27 16:37:20

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,686
Website

Re: Probably a simple problem, how should I return json?

The result is perfect valid JSON.
How do you return the content? For raw JSON, use a RawJSON method result.
Please debug on the JavaScript side, to find out what is actually parsed.

Offline

#3 2019-10-01 07:32:48

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Re: Probably a simple problem, how should I return json?

I am using JQUery's default parse command, nothing more and I know result is a valid JSON. But JSON has more then one valid formats. For example when i am using c# with mormot i had to remove brackets from to parse json into a class and also had to add JSONProperty["result"] to get it etc.

Which is fine, there is absolutely no problem with mORMot, maybe i should add braces to result.

Offline

#4 2019-10-01 08:10:04

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,686
Website

Re: Probably a simple problem, how should I return json?

I still don't understand what is your problem/concern, sorry.
You asked for a list, and you get a JSON array, as expected.

Offline

#5 2019-10-01 09:18:45

pvn0
Member
From: Slovenia
Registered: 2018-02-12
Posts: 211

Re: Probably a simple problem, how should I return json?

koraycayiroglu wrote:

I am using JQUery's default parse command

Have you tried using browser native JSON.parse() instead?

Last edited by pvn0 (2019-10-01 09:19:05)

Offline

#6 2019-10-02 09:24:34

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Re: Probably a simple problem, how should I return json?

ab wrote:

I still don't understand what is your problem/concern, sorry.
You asked for a list, and you get a JSON array, as expected.


I don't have a concern if i misguided you. I simply asking when i returning json to a web or mobile app how should i genuinely return json. like:

[{sample:1}, {sample:2}]

or

{sample:1}, {sample:2}

or

[result: { .. }]


to avoid cross-platform syntax errors.

If you don't understand what i meant, don't bother with it. I use mormot with multiple languages and platforms (swift, c#, jscript etc.) and i am trying to optimize my server for best.

Best regards

Offline

#7 2019-10-02 10:27:53

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Re: Probably a simple problem, how should I return json?

pvn0 wrote:
koraycayiroglu wrote:

I am using JQUery's default parse command

Have you tried using browser native JSON.parse() instead?


I will try that too, thanks.

Offline

#8 2019-10-14 08:57:04

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Re: Probably a simple problem, how should I return json?

I couldn't looked into this problem of mine for a while but at weekend i finally have some spare time to check what is going on. Here is my result;

I just realize I didn't need to parse my json with js because it's already parsed. Instead I use getJSON. Example:

            $.getJSON("/root/DefaultMethods/test_List", function( data ) {
                var rows = [];
                
                $.each( data["result"], function( key, val ) {
                    var items = [];
                    
                    $.each(val , function( propName, propVal ) {
                        items.push( "<li>" + propName + " : "+ propVal + "</li>");
                    });

                    rows.push("<ul>" + items + "</ul>");
                });

                $("#div1").html(rows);
            });

Tricky part is returned data val.

If I use:

    Server.RetrieveListJSON(TSQLTest, '', []);

I had to use " data.result[0] " to reach array.

but if I remove brackets before returning json as:

  Result := Server.RetrieveListJSON(TSQLJobHistory, '', []);
  Result := NSRemoveBrackets(Result); // personal function removing brackets

Then i can use as " data["result"] " to reach array as I already did in getJSON example.

As I told before, this is not a problem with mORMot. I just try to understand how this works. I am not a web developer so my js knowledge is not much. I posted this to help people like me. I think i will gonna keep removing brackets from result. It's much more easy to use from JS side.

Thank you all, best regards.

Offline

Board footer

Powered by FluxBB