0

What is the LiveCode equivalent of the following JavaScript code snippet?

            var req = new XMLHttpRequest();
            req.open("PUT", theURLoftheNoSQLstore, false);  
            req.setRequestHeader('Content-Type', mimeType);
            req.send(theJSONobjString); 

The parameters have been defined as

            theJSONobj   = {};
            theJSONobj["aKey"] = "aValue";
            theJSONobj["anotherKey"] = 123;
            theJSONobj["note"] = "some note about the issue";
            theJSONobjString = JSON.stringify(theJSONobj);
            theURLoftheNoSQLstore ="http://localhost:5984/thedb/thekey"

            mimeType="application/json";

Note

Setting the mimeType has been added for completeness. However for posting to the JSON store it is not necessary as that is the default in this case (couchDB).

References

mime type
XMLHttpRequest

Community
  • 1
  • 1
z--
  • 2,186
  • 17
  • 33

1 Answers1

2

The LiveCode equivalent of your JavaScript should be:

set the httpHeaders to "Content-Type: application/json"
put "[" into myJson
// add one record
put "{'key1':'data1','key2':'data2'}" after myJson
// don't forget commas between multiple records
put "]" after myJson
// next line may not work with more complex data
replace "'" with quote in myJson
// server must be able to process PUT
put myJson into URL "http://localhost:5984/thedb/thekey"
put the result into rslt
if rslt is not empty then
  beep
  answer error rslt
end if
Mark
  • 2,380
  • 11
  • 29
  • 49
  • works fine with couchdb in case you send a JSON object and not a JSON array. So only {"key1":"data1","key2":"data2"} and not [{"key1":"data1","key2":"data2"}] – z-- Aug 24 '13 at 11:32
  • I don't think that Hannes mentioned mobile anywhere, or did I overlook it? – Mark Aug 24 '13 at 22:20
  • No, you didn't it was just a note just in-case – Monte Goulding Aug 24 '13 at 23:53
  • I have tested it on Ubuntu 12.04 (LTS) with LiveCode 6.0 so far. [More](http://cnx.org/content/m33970/latest/) on the db used... – z-- Aug 25 '13 at 08:36