locked
WebView Ajax Not Working RRS feed

  • Question

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

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



    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>

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

    Thanks.

        
    Wednesday, December 25, 2013 1:31 AM

Answers

  • Hi Scott,

    I saw your ajax request is kind of cross domain, therefore you need to set crossDomain as true for your issue, by default it is false.

    crossDomain (default: false for same-domain requests, true for cross-domain requests)
    Type: Boolean

    If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true. This allows, for example, server-side redirection to another domain.

    And if crossDomain is not working, try to use following statement to get the error message.

    xhr.status + ": " + xhr.statusText

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Scott Deutsch Thursday, December 26, 2013 1:00 AM
    Wednesday, December 25, 2013 7:09 AM
    Moderator