javascriptarraysajaxlualuci

How to pass array in XHR.get request of Luci openwrt?


I am trying to send array from javascript to XHR.get request in Luci Openwrt .

var myarray[] has some contents . for example : `myarray[] = {"1","2","3"}` 

I need to pass this array to XHR.get requests as arguments . How can i pass this array ?

This is sample code :

XHR.get('<%=REQUEST_URI%>', {status: 2,value: myarray},
    function(x, info) {
        if (!info) {
            location.href=location.href;
        }
        else {
            console.log("success");
        }
    }
);

and in Lua code I am receiving this form data :

if luci.http.formvalue("status") == "2" then
    local DetailValue[] = luci.http.formvalue("value")
    local fileContent = {
        content = "sample1",
        find = "sample2"
    }
    luci.http.prepare_content("application/json")
    luci.http.write_json(fileContent)
    return
end

But I am getting errors. Is this correct way to send array via XHR.get ? Any suggestions ?


Solution

  • Here is an example for one value

    XHR.get('<%=url('admin/services/sample')%>',  { example : 'test' }, function(x) {
            <do whatever>
    });
    
    function parse_gateway()
        local example = luci.http.formvalue("example")
    end
    

    When I attempted to pass in two query parameters with the same key I got the the following

    XHR.get('<%=url('admin/services/sample')%>',  { example : 'test', example : 'test1' }, function(x) {
            <do whatever>
    });
    
    function parse_gateway()
        local example = luci.http.formvalue("example")
    end
    

    This time example was test1

    So in your case seems like you will have to have unique parameter keys.

    XHR.get('<%=url('admin/services/sample')%>',  { one : '1', two : '2', three : '3' }, function(x) {
            <do whatever>
    });
    
    function parse_gateway()
        local one = luci.http.formvalue("one")
        local two = luci.http.formvalue("two")
        local three = luci.http.formvalue("three")
    end