简体   繁体   English

无法在Android Webview中使用AJAX加载HTML部分

[英]Cannot load HTML partial with AJAX in android webview

I'm trying to load an HTML partial in my "app" that are running I an Android webview . 我正在尝试在运行Android webview “应用”中加载HTML部分。 Everything is made with HTML, CSS, and jQuery. 一切都由HTML,CSS和jQuery组成。

Every time my code is trying to load an HTMl partial it returns an error: 每次我的代码尝试加载HTMl部分代码时,都会返回错误:

XMLHttpRequest cannot load file:///W:/smarthubwebapp/public_html/html/pageHtml/login.html. XMLHttpRequest无法加载file:/// W:/smarthubwebapp/public_html/html/pageHtml/login.html。 Cross origin requests are only supported for protocol schemes: HTTP, data, chrome, chrome-extension, https, chrome-extension-resource. 跨源请求仅支持以下协议方案:HTTP,数据,chrome,chrome扩展名,https,chrome-extension-resource。

I know in this case it loaded in chrome and not in the Android webview I want to use in the end. 我知道在这种情况下,它加载为chrome,而不是最终要使用的Android webview

All the files that I use in the project local files so they are called with the "file:///" protocol in the webview. 我在项目本地文件中使用的所有文件,因此在Web视图中使用“ file:///”协议对其进行了调用。 Should it not work if I just open the index file in the browser and how do I fix this? 如果仅在浏览器中打开索引文件,该方法不起作用,该如何解决?

My ajax piece looks like this: 我的ajax片段如下所示:

$.ajax({
    url: "html/pageHtml/" + pageToLoad + ".html",
    type: "GET",
    crossDomain: true,
    dataType: 'html',
    success: function (response) {
        $("#pageContainer").html("<div id='innerPageContainer'>" + response + "</div>");
    },
    error: function (xhr, status) {
        $("#pageContainer").html("<div id='innerPageContainer'>" + JSON.stringify(status) + "</div>");
    }
});

You set the crossDomain property to true in the Ajax properties, hence the error. 您在Ajax属性中将crossDomain属性设置为true,因此会出现错误。

Simply remove that. 只需删除它。

The error says it all, 'File' requests can be used with crossDomain calls. 该错误说明了一切,“文件”请求可以与crossDomain调用一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM