Jump to content

callRemote callback question


Aibo

Recommended Posts

let's say i have some webpage that returns JSON array:

["1","value2","third value"]

and i can access these values in callback function just fine:

function remoteCallback(one,two,three)
end

my question is, how to get keys/values from a JSON object like this one:

{ "one" : "1", "two" : "value2", "three" : "third value" }

maybe i'm missing something, but i couldn't *(

and another question:

callRemote does not support GET method? cause every address with GET parameters returns curl error 22.

  • Like 1
Link to comment

No, callRemote doesn't support GET. If it did, you would be informed about it on function's wiki page.

{ } is as you know a table. So, in this case, Table.one or Table["one"] would return "1".

Where do you get this kind of JSON objects from anyway?

Link to comment

If i got what you want right you have two solutions for that:

1.

function remoteCallback(...)
local arguments = { ... }
end

here you can access your args in the arguments table like arguments[1], arguments[2], etc.

2.

Just pass your stuff in a table, json can cope with tables

Link to comment

well wiki indeed says POST, but not "POST only" or "GET is not supported". just wanted to be sure. :D

and yea, i know it's a table. and i wouldn't write here, haven't i tried all the ways i could think of, though i'm not a scripter :P

addCommandHandler("cr", 
function ()
callRemote("http://pixelterror.ru/json.php", remoteCallback)   
end
) 
 
function remoteCallback(aTable)
if aTable then
-- something is there 
outputChatBox("one: " .. aTable.one)
else
-- got nothing
outputChatBox(":(")
end
end

i got nothing there. and if i try to index aTable right away it obviously gives "attempt to index local aTable (a nil value)" error.

Where do you get this kind of JSON objects from anyway?

what do you mean by "this kind"? it's a common JSON object, "unordered set of name/value pairs". *)

and it's not covered in wiki, whether callRemote supports only JSON arrays. if it is - i've no further questions.

If i got what you want right you have two solutions for that:

1.

function remoteCallback(...)
local arguments = { ... }
end

here you can access your args in the arguments table like arguments[1], arguments[2], etc.

2.

Just pass your stuff in a table, json can cope with tables

yea i've tried that too, no luck:

function remoteCallback(...)
local aTable = {...}
if #aTable > 0 then
-- something is there 
outputChatBox("table: " .. table.concat(aTable,", "))
else
-- got nothing
outputChatBox(":(")
end
end

still ":(", cause it just makes an empty table.

the thing is, i've been asked to make a trace script (ehehe). and ran into 2 obstacles:

1. geoip site supports only GET, while callRemote only POST

2. geoip site returns JSON key/value object, and not an ordered array.

for now i have that taken care of by another php script which connects callRemote and geoip site.

i guess i'll have to setup own geoip database :S

Link to comment

You will need your own page - callRemote doesn't use a standard way of making requests - it sends the arguments as the JSON-encoded body of a POST request, which isn't a conventional thing to do - but provides more flexibility than a GET request could.

Link to comment

yeah, i figured that out about requests, thanks. but what about JSON that goes to callback function (since it was my main question), is there some restrictions concerning recieved JSON format?

everything is fine with arrays like [1,2,3] (and MTA PHP SDK returns data this way, as i recall), but how to get objects like {"key":"value"}?

am i missing something, or there's no way?

ofc it won't be an issue if i set up my own page, i'll just format JSON as needed or use the SDK.

just out of curiosity :P

Link to comment
yeah, i figured that out about requests, thanks. but what about JSON that goes to callback function (since it was my main question), is there some restrictions concerning recieved JSON format?

everything is fine with arrays like [1,2,3] (and MTA PHP SDK returns data this way, as i recall), but how to get objects like {"key":"value"}?

am i missing something, or there's no way?

ofc it won't be an issue if i set up my own page, i'll just format JSON as needed or use the SDK.

just out of curiosity :P

I've tried every possible thing I could imagine to retrieve the data from JSON objects, this is my thread.

https://forum.multitheftauto.com/viewtop ... 4&p=301555

I never figured it out though, so I just decided to send the info to the MySQL databases via php then retrieve them with the server's mysql connection. Takes more time, but it was the only way I could do it.

Link to comment
yeah, i figured that out about requests, thanks. but what about JSON that goes to callback function (since it was my main question), is there some restrictions concerning recieved JSON format?

everything is fine with arrays like [1,2,3] (and MTA PHP SDK returns data this way, as i recall), but how to get objects like {"key":"value"}?

am i missing something, or there's no way?

ofc it won't be an issue if i set up my own page, i'll just format JSON as needed or use the SDK.

just out of curiosity :P

I've tried every possible thing I could imagine to retrieve the data from JSON objects, this is my thread.

https://forum.multitheftauto.com/viewtop ... 4&p=301555

I never figured it out though, so I just decided to send the info to the MySQL databases via php then retrieve them with the server's mysql connection. Takes more time, but it was the only way I could do it.

why didn't you just format all the data as JSON ordered array, like

["74.125.45.100", "United States", "California", "Mountain View"]

if there was a problem with SDK doReturn, output the needed JSON string yourself with php echo

this way it's all passed in order to the callback variables, my current geolocation script runs like this. :D

Link to comment
If i got what you want right you have two solutions for that:

1.

function remoteCallback(...)
local arguments = { ... }
end

here you can access your args in the arguments table like arguments[1], arguments[2], etc.

....

Lua has a secret table when you have function with ... param which is arg, so you don't have to make new table like { ... } just use arg[1], arg[2], etc.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...