bndibx
August 3, 2018, 9:31am
1
Hello, I’m trying to open a link from my html5 game but I need to pass html data. Trying to use sys.open_url only allows me to pass a link, but I need to pass raw_html data like in webview.
Kindly assist.
My html data is like below:
local html_data = "<html>" ..
"<body onload='document.form_one.submit()'>" ..
"<div align='center'>" ..
"<h3>" ..
"Please Wait. You Will be Redirected Shortly<br/>" ..
"Don't Press Back" ..
"</h3>" ..
"</div>" ..
"<form method='post' action='https://somewhere.com/pay' name='form_one'>" ..
"<input name='product_id' type='hidden' value='2005' />" ..
"<input name='pay_item_id' type='hidden' value='150' />" ..
"<input name='amount' type='hidden' value='" .. data['amount'] * 100 .. "' />" ..
"<input name='currency' type='hidden' value='240' />" ..
"</form>" ..
"</body>" ..
"</html>";
Passing my data in webview.open_raw() on Android works perfectly but I need the same for web
britzl
August 3, 2018, 11:06am
2
The webview component is only available on Android and iOS. You could pass it as url encoded query argument:
sys.open_url("http://www.myurl.com?data=my_url_encoded_data")
Another option would be to use html5.run() and have javascript code in your index.html that does the job for you.
bndibx
August 3, 2018, 11:26am
3
Thanks for replying !
The url unfortunately only accepts POST data.
I’m trying to use html5.run() but it simply won’t accept my ajax. Here’s how I’m trying to do it.
local ajax_data =
"<script type='text/javascript'>" ..
"$(document).ready(function(){ " ..
"$.ajax({ " ..
"type:'POST', " ..
"cache:false," ..
"url:'https://somewhere.com/pay'," ..
"data : { " ..
"product_id : '2005', " ..
"pay_item_id : '150', " ..
"amount : '".. data['amount'] * 100 .."', " ..
"currency : '240'," ..
"txn_ref : '" .. txn_ref .. "' ," ..
"}" ..
"success: function (html) {" ..
" alert('Data was received sucessfully')" ..
"}" ..
"});" ..
"})"..
"</script>"
--here
html5.run(ajax_data).
I’m I missing something…
britzl
August 3, 2018, 8:45pm
4
html5.run() documentation says:
“Executes the supplied string as JavaScript inside the browser.”
You should not embed the javascript in a script tag. But I’m a bit confused in what you’re trying to do. Is the above the code you actually want to run? It’s a normal HTTP POST right? Why not do it from Lua using http.request()?
2 Likes
Pkeod
August 3, 2018, 10:34pm
5
Why not use http.request() directly?
1 Like
bndibx
August 4, 2018, 3:36pm
6
@britzl @Pkeod I can’t use defold’s http_request because my POST link redirects users to another portal where they need to fill in information hence i need a webview like component for html5 side of the game.
britzl
August 4, 2018, 9:34pm
7
Well, in that case I think you need to do the work through javascript in the embedding page, triggered via html5.run()
1 Like
bndibx
August 6, 2018, 9:06am
8
Thanks everyone. html5.run() works great for what I want.
Here’s a link to something that helped me a lot for anyone else :
https://stackoverflow.com/a/43482781/3729302
3 Likes