React Native Webview“onNavigationStateChange”仅在 iOS 设备上返回空 document.title
我正在尝试使用 Paypal 创建付款结账流程,并使用 WebView 处理 ExpressJS 后端的请求。 React Native应用程序读取后端渲染的html的document.title来确定事务的成功或失败。在 Android 上它工作得很好,但是在 iOS 上 document.title 总是返回为“”。
REACT NATIVE SECTION:
const INJECTED_JAVASCRIPT =
`document.getElementById('total').value="${total}";
document.getElementById('address').value="${address}";
document.getElementById('firstName').value="${userProfile.firstName}";
document.getElementById('lastName').value="${userProfile.lastName}";
document.getElementById('email').value="${accessStore.auth.userEmail}";
document.getElementById('addressDetails').value="${moreDetails}";
setTimeout(function() {document.f1.submit()}, 1000);`
<WebView
source={{uri: process.env.BACKEND_NODE_URL_PAYMENT}}
onNavigationStateChange={(data) => {
handleResponse(data), console.log(data)
}}
onMessage={(event) => {}}
injectedJavaScript={INJECTED_JAVASCRIPT}
/>
EXPRESS SECTION
app.get('/success', (req, res) => {
var PayerID = req.query.PayerID;
var execute_payment_json = {
"payer_id": PayerID,
};
var paymentId = req.query.paymentId;
paypal.payment.execute(paymentId, execute_payment_json, function (error, payment)
{
if (error) {
console.log(error.response);
throw error;
} else {
res.render('success');
}
});
});
在 EXPRESS JS 中呈现 HTML 的
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>success</title>
</head>
<body>
<h1>Zahlung erfolgreich!</h1>
</body>
</html>
I'm trying to create a payment checkout process with Paypal, and using WebView to handle the request on an ExpressJS backend. The React Native app reads the document.title of the rendered html in the backend to determine the success or failure of a transaction. On Android it works perfectcly, however on iOS the document.title is always returned as "".
REACT NATIVE SECTION:
const INJECTED_JAVASCRIPT =
`document.getElementById('total').value="${total}";
document.getElementById('address').value="${address}";
document.getElementById('firstName').value="${userProfile.firstName}";
document.getElementById('lastName').value="${userProfile.lastName}";
document.getElementById('email').value="${accessStore.auth.userEmail}";
document.getElementById('addressDetails').value="${moreDetails}";
setTimeout(function() {document.f1.submit()}, 1000);`
<WebView
source={{uri: process.env.BACKEND_NODE_URL_PAYMENT}}
onNavigationStateChange={(data) => {
handleResponse(data), console.log(data)
}}
onMessage={(event) => {}}
injectedJavaScript={INJECTED_JAVASCRIPT}
/>
EXPRESS SECTION
app.get('/success', (req, res) => {
var PayerID = req.query.PayerID;
var execute_payment_json = {
"payer_id": PayerID,
};
var paymentId = req.query.paymentId;
paypal.payment.execute(paymentId, execute_payment_json, function (error, payment)
{
if (error) {
console.log(error.response);
throw error;
} else {
res.render('success');
}
});
});
RENDERED HTML IN EXPRESS JS
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>success</title>
</head>
<body>
<h1>Zahlung erfolgreich!</h1>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我使用Web视图的
onLoadProgress
属性通过e.nativeEvent
获取URL:I used the
onLoadProgress
property of web view to get URL throughe.nativeEvent
:最后用不同的方法解决了。我使用了“url”值并在条件中使用它
Solved with a different approach in the end. I used the "url" value and used it in the conditional as
根据我在 WebView 文档中读到的内容,为了将消息从 Web 传递到 React Native,您必须在 JavaScript 中使用
postMessage
,在 React 中使用onMessage
prop本机: https://github.com/react-native-webview/react-native-webview/blob/master/docs/Guide.md#communicating- Between-js-and-native这在理论上意味着你的代码即使在 Android 上也不应该工作,所以你肯定有一个有趣的用例!
From what I've read in the WebView docs, in order to pass messages from Web to React Native you'll have to use
postMessage
in JavaScript and tehonMessage
prop in React Native: https://github.com/react-native-webview/react-native-webview/blob/master/docs/Guide.md#communicating-between-js-and-nativeThis means in theory your code should not have worked even on Android, so you definitely have an interesting use case!
我在最近的一个应用程序中遇到了同样的问题,其中 html 是从 React-native 的后端处理的,
请原谅它不是 webview,
我使用
react-native-render-html
包来渲染收集的 html来自后端I faced the same in one of my recent apps where html is handled from backend in react-native,
pardon its not webview
i used
react-native-render-html
package to render the html collected from backend