San*_*kil 7 post webview flutter
我想将一些数据发布到 Flutter WebView 中的 URL 主体。那么,我该怎么做呢?
Lor*_*lli 13
webview_flutter目前没有发送 post 请求的方法。\n但是,您可以尝试我的flutter_inappwebview插件。它支持POST请求!
使用当前最新版本5.0.5+3的flutter_inappwebview插件的一个简单示例是:
var postData = Uint8List.fromList(utf8.encode("firstname=Foo&lastname=Bar"));\ncontroller.postUrl(url: Uri.parse("https://example.com/my-post-endpoint"), postData: postData);\nRun Code Online (Sandbox Code Playgroud)\n其中postData是格式中请求的正文x-www-form-urlencoded。
例如,如果您有 PHP 服务器,则可以像平常一样访问firstname\xc2\xa0and值,即和。lastname$_POST[\'firstname\']$_POST[\'lastname\']
您还可以使用初始 POST 请求来初始化InAppWebView\xc2\xa0widget,如下所示:
child: InAppWebView(\n initialUrlRequest: URLRequest(\n url: Uri.parse("https://example.com/my-post-endpoint"),\n method: \'POST\',\n body: Uint8List.fromList(utf8.encode("firstname=Foo&lastname=Bar")),\n headers: {\n \'Content-Type\': \'application/x-www-form-urlencoded\'\n }\n ),\n onWebViewCreated: (controller) {\n \n },\n),\nRun Code Online (Sandbox Code Playgroud)\n
您可以在Flutter官方WebView上运行JS功能::
1-JS POST请求示例
**
* sends a request to the specified url from a form. this will change the window location.
* @param {string} path the path to send the post request to
* @param {object} params the parameters to add to the url
* @param {string} [method=post] the method to use on the form
*/
function post(path, params, method='post') {
// The rest of this code assumes you are not using a library.
// It can be made less verbose if you use one.
const form = document.createElement('form');
form.method = method;
form.action = path;
for (const key in params) {
if (params.hasOwnProperty(key)) {
const hiddenField = document.createElement('input');
hiddenField.type = 'hidden';
hiddenField.name = key;
hiddenField.value = params[key];
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
}
Run Code Online (Sandbox Code Playgroud)
2-在 Flutter 中将 JS 函数转换为字符串并调用
final String postUrl="'https://jsonplaceholder.typicode.com/posts'";
final String postParam= "{name: 'Johnny Bravo'}";
final String requestMethod= "'post'";
final String jsFunc="function post(path, params, method='post') {"+
"const form = document.createElement('form');"+
"form.method = method;"+
"form.action = path;"+
"for (const key in params) {"+
"if (params.hasOwnProperty(key)) {"+
"const hiddenField = document.createElement('input');"+
"hiddenField.type = 'hidden';"+
"hiddenField.name = key;"+
"hiddenField.value = params[key];"+
"form.appendChild(hiddenField);}}document.body.appendChild(form);form.submit();}"+
"post($postUrl, $postParam, method=$requestMethod)";
Run Code Online (Sandbox Code Playgroud)
3-在WebView中运行JS代码
return FutureBuilder<WebViewController>(
future: _controller.future,
builder: (BuildContext context,
AsyncSnapshot<WebViewController> controller) {
if (controller.hasData) {
return FloatingActionButton(
onPressed: () async {
controller.data!.evaluateJavascript(jsFunc);
},
child: const Icon(Icons.call_received),
);
}
return Container();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15265 次 |
| 最近记录: |