#1 2015-09-11 13:00:23

dolfin
Member
Registered: 2015-09-11
Posts: 22

Get true JSON object on AJAX request

I faced with issue dealing with transmitted JSON content from server to ajax client application.
I have the following code:

type
TPersist = class(TPersistent)
   private
    FFileName     : RawUtf8;
    FDisplayName  : RawUtf8;
  published
   property FileName     : RawUtf8  read FFileName     write FFileName;
   property DisplayName  : RawUtf8  read FDisplayName  write FDisplayName;
  end;

TSQLBaby = class(TSQLRecord)
private
  fName: RawUTF8;
  FPersistField: TPersist;
published
  property Name: RawUTF8 read fName write fName;
  property PersistField : TPersist read FPersistField write FPersistField ;
end;

p:=TPersist.Create;
Baby.FPersistField := p;
p.FFileName := 'This is file name';
p.FDisplayName := 'This is display name';
Client.Add(Baby,true);

And by going to uri: http://localhost:8080/root/baby/26 I get the following answer:
{"ID":26,"Name":"TEST_COL_LIST","PersistField":"{\"FileName\":\"This is file name\",\"DisplayName\":\"This is display name\"}"}

The issue is that I get PersistField as string"{\"FileName\":\"This is file name\",\"DisplayName\":\"This is display name\"}"} but I want to get a true JSON object.

I found next info in the manual:
Up to revision 1.15 of the framework, the transmitted JSON content was not a true JSON object, but
sent  as  RawUTF8  TEXT  values  (i.e.  every  double-quote  (")  character  is  escaped  as   -  e.g.
"UTF8":"[32]").  Starting  with  revision  1.16  of  the  framework,  the  transmitted  data  is  a  true  JSON
object, to allow better integration with an AJAX client. That is, UTF8 field is transmitted as a valid JSON
array of string, and Persistent as a valid JSON object with nested objects and arrays.

As I understood, since I use version 1.18 I should receive a true json object.

Could you please help me on this issue?

Offline

#2 2015-09-11 13:26:26

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

Re: Get true JSON object on AJAX request

Your quote is about the transmission from client to the server.
In this case, you can send plain string or an object to the server.

In the other direction ,i.e. transmission to client from the server, if the object is retrieved directly from the database, it may reflects the storage layout, which is a string, depending on the database.

Offline

#3 2015-09-13 16:44:43

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

Re: Get true JSON object on AJAX request

AJAX clients will now be identified so that /tablename/id would return true JSON, e.g. for nested objects, even if was stored as string or binary.
See http://synopse.info/fossil/info/04598ad7c4

Hope it helps.

Offline

#4 2015-09-14 15:11:22

dolfin
Member
Registered: 2015-09-11
Posts: 22

Re: Get true JSON object on AJAX request

Greate news, thank you very much for a fast fix!
You did this fix for  /tablename/id query, is it possible to add this improvement for /tablename/?select=* query also? I would be very grateful if you would do it.

Offline

#5 2015-09-15 07:46:32

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

Re: Get true JSON object on AJAX request

We have ensured that root/table?select=...&where=... return plain standard JSON output for AJAX clients.

See http://synopse.info/fossil/info/e07ad8729f

Offline

#6 2015-09-15 21:16:39

dolfin
Member
Registered: 2015-09-11
Posts: 22

Re: Get true JSON object on AJAX request

That's awesome, great thx for that!
And I have one more question, dealing ajax. Is it possible to return some data, for example ID, after POST request?

Offline

#7 2015-09-15 21:20:04

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

Re: Get true JSON object on AJAX request

The ID is part of the headers.

Offline

Board footer

Powered by FluxBB