[Webkit-unassigned] [Bug 284516] After I used WKWebView to load a local HTML file, I tried to initiate an interface request to an external domain name through Axios, and a cross-domain problem occurred
bugzilla-daemon at webkit.org
bugzilla-daemon at webkit.org
Mon Dec 16 09:54:52 PST 2024
https://bugs.webkit.org/show_bug.cgi?id=284516
Alexey Proskuryakov <ap at webkit.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Summary|我在使用 WKWebView 加载本地 HTML |After I used WKWebView to
|文件后,尝试通过 Axios |load a local HTML file, I
|发起对外部域名的接口请求,出现了跨域问题 |tried to initiate an
| |interface request to an
| |external domain name
| |through Axios, and a
| |cross-domain problem
| |occurred
CC| |charliew at apple.com,
| |m_finkel at apple.com,
| |wilander at apple.com
--- Comment #2 from Alexey Proskuryakov <ap at webkit.org> ---
Google translation of the description:
1 After I used WKWebView to load a local HTML file, I tried to initiate an interface request to an external domain name through Axios, and a cross-domain problem occurred. My local file was loaded through the loadFileURL:allowingReadAccessToURL: method, and the interface requested by Axios was such a domain name as https://fundaccount.chinaclear.cn/api, and the browser returned a cross-domain error . ” What needs to be confirmed is that our server does not support all cross-domain operations and cannot set Access-Control-Allow-Origin
2 Then I solved this problem by setting allowfileAccessFromFileURLs to YES
3 But if the iPhone device turns on the lock mode. You can turn on this mode in the settings. At this time, the second step will not work, resulting in a cross-domain error
4 Now we implement it through WKURLSchemeHandler, and the specific code for custom sheme interception is (void)webView:(WKWebView *)webView startURLSchemeTask:(id<WKURLSchemeTask>)urlSchemeTask {
NSURL *url = urlSchemeTask.request.URL;
NSLog(@"jess test%@", url);
if ([url.pathExtension isEqualToString:@"html"] && [url.host isEqualToString:@"fundaccount.chinaclear.cn"]) {
NSString* propertyPath = [[NSBundle mainBundle] pathForResource:@"" ofType:@"html" inDirectory:@"asset"];
NSData *data = [[NSData alloc] initWithContentsOfFile:propertyPath];
NSMutableDictionary *resHeader = [NSMutableDictionary new];
[resHeader setValue:@"*" forKey:@"Access-Control-Allow-Origin"];
[resHeader setValue:@"charset=UTF-8" forKey:@"Content-Type"];
[resHeader setValue:@"text/html" forKey:@"Content-Type"];
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:url statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:resHeader];
[urlSchemeTask didReceiveResponse:response];
[urlSchemeTask didReceiveData:data];
[urlSchemeTask didFinish]; This method
5 This method solves the problem of loading local files across domains. I want to confirm whether this method is supported by Apple to solve the problem of loading local files across domains.
6 Now, after consulting the documentation, it is shown that this method also has some problems. I hope you can provide an official answer to solve this problem.
7 I also want to ask about the situation I encountered. It is mainly about the cross-domain problem after setting allowfileAccessFromFileURLs when the phone is in lock mode. Do you know this situation?
8 I hope you can provide an official solution to the problem I encountered.
--
You are receiving this mail because:
You are the assignee for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-unassigned/attachments/20241216/c614fcdf/attachment-0001.htm>
More information about the webkit-unassigned
mailing list