Passing an array to the server using Ajax


#1

Looking at the Ajax.nsx example, it almost does exactly what I need.
However, my requirement is to store 10 short text strings on the server.
Is it possible to send an array such as myTextArray() instead of the single string ‘myText’
Or is there a better way to store the 10 strings.


#2

Json stringify the array and send that. Parse it back.


#3

Thank you. Forgive my lack of knowledge but how do I actually do that.
I know how to create an array and slice out the elements subsequently.
Is there an example of what you suggest wrt Json. I’m afraid I don’t understand.

I see there is an example of reading back a Json file into an array. (AjaxGetWebFile.nsx)
How do I create the Json array, please.

I found the answer:

'Create the array
dim data[12]
MyJSON=JSON.stringify(data) 
'Send it
req=Ajax("ajax.php/?myText=" + MyJSON, done)

Thanks for the pointer to the solution


#4

I have this working now using AjaxPost to save a file to the sever and req=ReadFile(filename); to retrieve it.
The posting of the file to the server works fine - I can inspect the contents using Filezilla and the new values are shown.
However, the problem that I have is that it seems that once the file has been retrieved, it gets stuck. If the file is updated, no amount of browser refreshes or relaunches show the updated version when read. Opening in another browser displays the new values but that also gets stuck. It’s as if there is a cached version held in the app which never gets updated.
Checking req.status shows a successful read so I’m baffled as to what can be going wrong.

This is the ‘Read’ code

    strLoginList = ""
    filename = "logins.txt";
    req = ReadFile(filename);
    if (req.status == 200) {
        strLoginList = req.responseText;
        alert("read reply: " + strLoginList);
        SplitLoginFile();
    } else {
        alert(req.err);
    }

strLoginList persists in returning the last values regardless of the content on the server.
Am I missing something obvious - any suggestions welcomed.


#5

Solution found:

I had omitted adding “Post” as the ‘method’. Clue - read the manual.
Use method to define the access method. Usually this parameter can be left out: the default is Get. The next most common value for this is Post. Post responses are never cached, whereas Get responses can be

req = ReadFile(filename, “Post”);