java - Android 通过 Webview POST 表单数据
问题描述
我的 html 代码中有以下实现。
<!doctype html>
<html ng-app="app">
<head>
<title>post data</title>
</head>
<body>
<div>
<form action="https://mypaymentgateway/pay" method="post">
<input type="hidden" id="profile_id" name="profile_id" value="123" />
<input type="hidden" id="transaction_id" name="transaction_id" value="abcd" />
<input type="hidden" id="locale" name="locale" value="en" />
<input type="submit" id="btnsubmit" value="Confirm" />
</form>
</div>
</body>
</html>
我想要做的是通过调用我的付款网址(“https://mypaymentgateway/pay”)的 POST 方法在 webview 中提交上述值,而无需点击“确认”按钮。我已经在这里和这里以及其他来源尝试过解决方案。但似乎没有任何工作正常。请帮我解决这个问题。
解决方案
form.submit()
如果您想在不按下按钮的情况下发布表单,请尝试自行调用
根据您的需要,有两种方法可以做到这一点,通过 HTML 文件本身的 JavaScript 或通过启用 WebView 组件的 Java 文件。
通过 HTML 文件中的 JavaScript:
<form id="myForm" action="https://mypaymentgateway/pay" method="POST"> ... </form>
<script>
function submitMyForm() {
var myForm = document.querySelector("#myForm");
myForm.submit();
}
</script>
您只需submitMyForm()
在需要的地方调用 JavaScript。
通过 Java 文件(在 HTML 文件中的 JavaScript 的全局范围内定义前面的函数):
//First enable your JavaScript
webView.getSettings().setJavaScriptEnabled(true);
//And then with a loadURL you can trick the WebView into executing Javascript
webView.loadUrl("javascript:submitMyForm");
推荐阅读
- javascript - 输入字段自动建议在值不匹配后仍然显示:Vanilla JS
- r - Shiny App可点击点不适用于对数刻度y轴
- python - 如果时间输入在上午 8 点之前和上午 12 点之后,请将 pandas 中 datetime 列中的时间更改为特定时间
- docker - Docker:不允许附加属性 pull_policy
- java - 如何在按钮切换组中调整按钮大小
- javascript - 通过 javascript 设置的 Firefox CSS 缓存问题
- python - 如何在 cv2 中从 Tensorflow 的对象检测 API 运行 ssd_resnet_50_fpn_coco 网络?
- c++ - 使用 nlohmann json 创建嵌套 json 对象数组
- php - 如果至少有一篇文章可用,请添加元框
- javascript - 当我测试响应性时,页面使背景图片太小,需要帮助使我的 html 页面响应 CSS