B4J Question Handling Javascript POST request on server.

jimmyF

Active Member
Licensed User
Longtime User
I am porting a WebApp(Javascript) -> PHP -> MySQL system to WebApp(Javascript) -> jServer-> MySQL.
I have no problems with the jServer-> MySQL part.

I would prefer to use a socket handler class as opposed to a request/response handler class but that is not really important.

I want to simply send an ID and get the data value back, preferably as text.

Questions:
1. Do I need to create an index.html file in the 'www' folder to do this? And what does it contain? There is no UI. (I am looking at the Server Example)

2. How do I structure my Ajax call and how do I receive a POST or GET on the server?

I have looked at many examples but they are all B4X -> B4J.
I thought it would be something simple like the 'Action' handler in the CloudKVS example but I don't seem to be able to connect. What should my URL look like?

All server permissions and authentication systems are working fine.

Any push in the right direction would be much appreciated.

Thanks
JimmyF
 

jimmyF

Active Member
Licensed User
Longtime User
I got it!
 
Upvote 0

jimmyF

Active Member
Licensed User
Longtime User
Okay, maybe not. I can send one record of a ResultSet as json by creating a Map as follows
using JSONGenerator.

B4X:
'code here to fill the map

js.Initialize(myMap)
resp.ContentType = "application/json"
resp.Write(js.ToString)


How do I send the whole ResultSet?

Do I create a new map and add all the records to it?
Tried it. It does not work.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Do I create a new map and add all the records to it?
Tried it. It does not work.
Why does it not work? How about some code on how you read the result set and create the map?
 
Upvote 0

jimmyF

Active Member
Licensed User
Longtime User
I should have checked the Chrome console.logs before asking the question.

It seems I am AM getting the data.

The problem is not a B4X problem.

I am getting a json parsing error in the Ajax returnFunction which I need to figure out.
"SyntaxError Unexpected token { in JSON at position 283"

I tried converting my text data to base64 before generating the json but the error is the same so, I don't believe, it is not the data that is at fault.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
1. Create a list
2. Iterate through all Results of the reultset.
2b. Create a Map for one Result of the resultset. Add the Map to the list.
3. After the iteration. generate a json from the list holding the maps.
B4X:
jsongen.initialize2(thelist)
 
Upvote 0

jimmyF

Active Member
Licensed User
Longtime User
That was it. Exactly!

Thank you so much @DonManfred

I was not aware of the 'initialize2' of the JsonGenerator.

I love the B4X products. There is always something new to learn.
 
Upvote 0
Top