【问题标题】:WebView Windows Store App C# Ajax Not WorkingWebView Windows Store App C# Ajax 不工作
【发布时间】: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>

【问题讨论】:

    标签: c# html ajax xaml webview


    【解决方案1】:

    原来在 ajax 中我有数据类型而不是数据类型。差异使它起作用了。奇怪的是,Android 和 iOS 不关心它是数据类型还是数据类型。但现在可以了!

    谢谢。

    【讨论】:

      【解决方案2】:

      你的 ajax 调用应该是

            $.ajax({
                  type: "GET",
                  url: "fake_domain_here/api/v1/api_call",
                  data: "{}",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  success: function (data) {
                     alert(data.d);
                  },
                  error: function () { alert(arguments[2]); }
              });
      

      【讨论】:

      • 还是不行。只是为了让您知道我的代码适用于 Android 和 iOS。它只是不适用于 Windows 8.1 WebView XAML。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多