OpenWrt Forum Archive

Topic: Luci.http.write in Controller Question

The content of this topic has been archived on 21 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hello,

I'm trying to call a function in my controller from one of my views by using the XHR.poll method, and have the call return some uci data from my /etc/config/ configuration file.  However, I'm not successful, and can't really figure out what's going on.

Right now, the javascript in my view code looks like this:

function myFunction() { 
    XHR.poll(5, '<%=luci.dispatcher.build_url("admin", "package", "call")%>/', null, function(x, data) {
        alert("Data is " + data);
    }); 
};

My controller call looks like this:

function action_my_function()
    local uci = luci.model.uci.cursor()

    luci.http.prepare_content("text/plain") 
    luci.http.write(uci:get("package", "section", "option"))
end

Also, here is my entry code, but I don't think the problem lies here at all:

    entry({"admin", "package", "call"}, call("action_my_function"), nil).leaf = true

I can see that the luci.http.write returns what I like (that is, the correct configuration option value in my configuration file) by navigating to "http://NODE/cgi-bin/luci/admin/package/call" in my browser, however, on the actual view page, if I set a breakpoint in my javascript code, Firebug tells me that my "data" variable is null.

I'm sure I'm doing something simple completely wrong, but I can't figure out what.  Does anyone have any clue?  Is there a more elegant way of doing what I want instead of using luci.http.write?  Like, can I just use a 'return' in the controller Lua code?  I've tried this method before, but to no avail.

As always, thanks in advance for any insight.

--
Justyn

I can figure it out that what you are doing. You may change this line :

luci.http.write(uci:get("package", "section", "option"))

I look up in the reference that luci.http.write , the first argument is the name of the package, second is name of section and the  third is name of option which is optional. Check this line. You offer just the strings.  Check this line. And sometime the section is anonymous so take care of it.

I hope it will help.

(Last edited by Wandy on 11 Jun 2014, 03:52)

Wandy,

Thanks for taking a look at the post.  uci:get will return a string if the option exists, or nil if it does not.  I've verified that it does indeed return the (correct) string when invoked (by debug dumps, etc). luci.http.write takes a "content chunk" (whatever that means) and returns it to the client.  If this is nil, luci.http.close is invoked.  I can confirm that luci.http.write is being invoked by navigating to http://NODE/cgi-bin/luci/admin/package/call in my brower, however, it never makes it to the JavaScript code, which leads me to believe I'm doing something wrong there, and not in the Lua namespace.

Albeit, I should refactor the code so that nil is handled better (instead of just passing it along), and I should look more into what a content chunk, and what a LTN12 Sink is.  The examples I've seen, however, show luci.http.write being invoked with a string literal as the parameter.  It might not be that easy though.

The "data" argument passed to the callback function is only defined if the response was of type "application/json" - in this case "data" holds the decoded JSON response readily accessible for JavaScript.

Since you're sending non-JSON data you need to access the raw response using the xhr object passed as first argument, so replace

alert("Data is " + data);

with

alert("Data is " + x.responseText);

Ah! I now see that in Firebug x contains all the relevant info... I either missed it or skipped over it when I was looking at it before, and only focused on why data was always nil.

As always, thanks for the help, jow.  This is my first venture into an MVC scheme, but I think I'm starting to get used to the LuCI stuff and am really enjoying its huge power for such a lightweight package.

--
Justyn

You are welcome.Iam wondering how  to pass an argument to that function.
What if I want to get some input from user then pass it to the function(in your example is "action_my_function"). How to achieve that?

Wandy,

I guess I unsubscribed to this post because I didn't see your reply.

You asked what I have previously asked in a different thread.  Jow helped with a solution there.

https://forum.openwrt.org/viewtopic.php?id=49899

Look at my question, reply 4, and his answer, reply 5.

justynnuff wrote:

Wandy,

I guess I unsubscribed to this post because I didn't see your reply.

You asked what I have previously asked in a different thread.  Jow helped with a solution there.

https://forum.openwrt.org/viewtopic.php?id=49899

Look at my question, reply 4, and his answer, reply 5.

Got it. jow has replied to me. Thank you. I am working in luci . I wonder if we can discuss about some problems. Can you leave me your email or some thing we can get in touch with each other for the communication.

Best wishes.

The discussion might have continued from here.