如何在 Flutter WebView 中 Post 数据到 URL
How to Post Data to URL in Flutter WebView
我想 post 一些数据到 Flutter WebView 中的 URL 主体。
那么,我该怎么做呢?
webview_flutter
目前没有发送 post 请求的方法。
不过,您可以试试我的 flutter_inappwebview 插件。它支持 POST 个请求!
使用 flutter_inappwebview 插件的当前最新版本 5.0.5+3
的一个简单示例是:
var postData = Uint8List.fromList(utf8.encode("firstname=Foo&lastname=Bar"));
controller.postUrl(url: Uri.parse("https://example.com/my-post-endpoint"), postData: postData);
其中 postData
是 x-www-form-urlencoded
格式的请求正文。
例如,如果您有一个 PHP 服务器,您可以像往常一样访问 firstname
和 lastname
值,即 $_POST['firstname']
和 $_POST['lastname']
.
您还可以使用初始 POST 请求初始化 InAppWebView
小部件,如下所示:
child: InAppWebView(
initialUrlRequest: URLRequest(
url: Uri.parse("https://example.com/my-post-endpoint"),
method: 'POST',
body: Uint8List.fromList(utf8.encode("firstname=Foo&lastname=Bar")),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
),
onWebViewCreated: (controller) {
},
),
你可以 运行 官方 Flutter 上的 JS 函数 WebView ::
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();
}
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)";
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();
});
我想 post 一些数据到 Flutter WebView 中的 URL 主体。 那么,我该怎么做呢?
webview_flutter
目前没有发送 post 请求的方法。
不过,您可以试试我的 flutter_inappwebview 插件。它支持 POST 个请求!
使用 flutter_inappwebview 插件的当前最新版本 5.0.5+3
的一个简单示例是:
var postData = Uint8List.fromList(utf8.encode("firstname=Foo&lastname=Bar"));
controller.postUrl(url: Uri.parse("https://example.com/my-post-endpoint"), postData: postData);
其中 postData
是 x-www-form-urlencoded
格式的请求正文。
例如,如果您有一个 PHP 服务器,您可以像往常一样访问 firstname
和 lastname
值,即 $_POST['firstname']
和 $_POST['lastname']
.
您还可以使用初始 POST 请求初始化 InAppWebView
小部件,如下所示:
child: InAppWebView(
initialUrlRequest: URLRequest(
url: Uri.parse("https://example.com/my-post-endpoint"),
method: 'POST',
body: Uint8List.fromList(utf8.encode("firstname=Foo&lastname=Bar")),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
),
onWebViewCreated: (controller) {
},
),
你可以 运行 官方 Flutter 上的 JS 函数 WebView ::
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();
}
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)";
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();
});