none
AJAX in Microsoft Office Web App RRS feed

  • Question

  • I was coding an app for Office, where i planned to use AJAX to load contents dynamically from a external website to the office app. I have the following JS  function. When the execution reaches to `a.open()` function, it shows an error:

         Unhandled exception at line 31, column 21 in https://localhost:44304/App/Home/Home.js

         0x80070005 - JavaScript runtime error: Access is denied.

    -

    function getDataFromSelection() {
            Office.context.document.getSelectedDataAsync(Office.CoercionType.Text,
                function (result) {
                    if (result.status === Office.AsyncResultStatus.Succeeded) {
    
                        var a = new XMLHttpRequest();
                        a.onreadystatechange = function () {
                            if (a.readyState == 4 && a.status == 200) {
                                var jSONstr = a.responseText;
                                var obj = jQuery.parseJSON(jSONstr);
                                showSearch(obj);
                            }
                        };
    
                        a.open("GET", "http://some.external.site/page.php?query=" + result.value, true); //This has got the error
                        a.send();
                    }
                    else {
                        app.showNotification('Damn!:', result.error.message);
                    }
                }
            );
        }
    

        



    Is AJAX not supported in Office Apps? How can i do the exact thing the other (working) way? 

    PS: I am using Visual Studio 2012 On Win8

           
    Saturday, April 27, 2013 5:50 PM

Answers

  • If jsonp or CORS are not possible solutions (web service is out of your control), then the only thing i think can work is to write your on web service and proxy the target web service and wrap the result to jsonp or CORS.

    Anze Javornik

    Monday, April 29, 2013 8:09 AM

All replies

  • For simpler ajax calls use jquery where an ajax call looks like this:

    $.get('example/file.html', function(data) {
        alert(data);
    });

    For more info check jquery ajax documentation.

    For ajax calls to a different domain either:

    • use dataType: 'jsonp' in ajax call and add jsonp support to server side
    • or in CORS supported browsers (IE10, Firefox, Chrome, ... IE9 or older are not CORS supported) make sure server side is also CORS supported

    Also note that ActiveX objects are not allowed in Apps for Office.


    Anze Javornik

    Sunday, April 28, 2013 11:22 PM
  • I am getting the same error as well rehpic.

    I still have problems when doing what you propose Anze - JQuery get does not work either. I am calling someone else's service - so I cannot "add jsonp support to the server side" if it doesn't already exist.

    More details here: http://social.msdn.microsoft.com/Forums/en-US/appsforoffice/thread/0f487559-4c32-4ec6-a5de-c829e704875d

    Monday, April 29, 2013 5:04 AM
  • If jsonp or CORS are not possible solutions (web service is out of your control), then the only thing i think can work is to write your on web service and proxy the target web service and wrap the result to jsonp or CORS.

    Anze Javornik

    Monday, April 29, 2013 8:09 AM