WebView Windows应用商店C#Ajax无法正常工作 [英] WebView Windows Store App C# Ajax Not Working

查看:40
本文介绍了WebView Windows应用商店C#Ajax无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ajax不能在XAML WebView中工作吗?每当我进行Ajax调用时,它总是运行错误回调.

Does Ajax not work in a XAML WebView? Whenever I make an Ajax call it always runs the error callback.

仅供参考,这里是test.js代码:

Just for your reference here is the test.js code:

$(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文件:

test.html file:

<!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文件中,我这样做

In the XAML file I do this

<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问题?

What do you guys suggest I try to see if that fixes the ajax issue?

更新1

所以这听起来像是跨域问题.解决该问题的最佳方法是什么?

So it sounds like a Cross Domain issue. What is the best way to go around that?

我已经在服务器上安装了这个,所以我认为应该解决任何跨域问题...除非设置错误?

I already have this on my server so I thought that should of fixed any cross domain problems...unless it is set wrong?

<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>

推荐答案

事实证明,在ajax中,我具有数据类型,而没有dataType.差异使它奏效.奇怪的是,Android和iOS不在乎它是datatype还是dataType.但是现在可以了!

Turns out that in the ajax I had datatype and not dataType. The difference made it worked. That is strange that Android and iOS doesn't care if it is datatype or dataType. But now it works!

谢谢.

这篇关于WebView Windows应用商店C#Ajax无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆