【发布时间】:2014-01-09 22:41:01
【问题描述】:
Ajax 不能在 XAML WebView 中工作吗?每当我进行 Ajax 调用时,它总是运行错误回调。
test.js 代码仅供参考:
$(function () {
$.ajax({
url: "http://fake_domain_here/api/v1/api_call", //I use a real domain name...I just removed it here.
data: {
id: 1
},
type: "GET",
datatype: "json",
timeout: 30000,
success: function (data, textStatus, xhr) {
window.external.notify("HERE 1");
},
error: function (xhr) {
window.external.notify("HERE 2"); //always goes here.
}
});
});
test.html 文件:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="js/test.js"></script>
</head>
<body>
<h1>In here.</h1>
</body>
</html>
在 XAML 文件中我这样做
<Page
x:Class="APPNAMECLASSHERE"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:LSWApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<WebView x:Name="wv" HorizontalAlignment="Left" Height="748" VerticalAlignment="Top" Width="1346" Margin="10,10,0,0" ScriptNotify="script_notify" Source="ms-appx-web:///Assets/www/test.html"/>
</Grid>
</Page>
你们有什么建议我试试看是否能解决 ajax 问题?
更新 1
所以这听起来像是一个跨域问题。解决这个问题的最佳方法是什么?
我的服务器上已经有这个,所以我认为应该解决任何跨域问题...除非设置错误?
<VirtualHost *:80>
ServerName NAME
ServerAlias ALIAS
DocumentRoot DIR
ErrorDocument 503 /system/maintenance.html
RewriteEngine On
RewriteCond %{REQUEST_URI} !.(css|gif|jpg|png)$
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ - [redirect=503,last]
Header set Access-Control-Allow-Origin "*"
</VirtualHost>
【问题讨论】: