2013-12-21 71 views
0

Ajax在XAML WebView中不起作用嗎?每當我進行Ajax調用時,它總是運行錯誤回調。WebView Windows應用商店應用C#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> 

回答

0

原來,在阿賈克斯我有數據類型,而不是數據類型。差異使它工作。奇怪的是,Android和iOS不關心它是數據類型還是數據類型。但現在它起作用了!

謝謝。

0

你的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]); } 
     }); 
+0

仍然不起作用。只是爲了讓你知道我的代碼在Android和iOS上工作。它只是在Windows 8.1 WebView XAML上不起作用。 –