none
getJSON for office apps RRS feed

  • Question

  • Hi

    I'm developing a WORD task pane app. I have some troubles with loading a JSON file. My app works fine if I use next functions

    // Reads data from current document selection and displays a notification
        function getDataFromSelection() {
            Office.context.document.getSelectedDataAsync(Office.CoercionType.Text,
                function (result) {
                    if (result.status === Office.AsyncResultStatus.Succeeded) {
                        showImages(result.value);
                    } else {
                        app.showNotification('Error:', result.error.message);
                    }
                }
            );
        }
    
    
        function showImages(result) {
    
            $('#Images').empty();
    
            var parameters = {
                tags: result,
                tagsmode: "any",
                format: "json"
            };
    
            $.getJSON("https://secure.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
                            parameters,
                            function (results, succes) {
                                app.showNotification(succes);
                                $.each(results.items, function (index, item) {
                                    $('#Test').append($("<img />").attr("src", item.media.m));
                                });
                            }
            );
        }
    

    I get some Flickrs pictures.
    But if I try another example

        function johnDoe() {
            $.getJSON("http://www.w3schools.com/jquery/demo_ajax_json.js",
                function (results, succes) {
                    app.showNotification(succes);
                    $.each(results, function (i, field) {
                        $("#Test").append(field + " ");
                    });
                });
        }
    

    I don't even get an notification with 'succes' or 'timeout'. Can someone help me out?
    It is the last step in my project, and I'm already a long time busy with searching and trying.

    Thank you very much,

    Emiel


    thanks, Emiel

    Thursday, August 13, 2015 7:28 PM

Answers

  • Hi Emiel,

    As far as I test, when I put the JavaScript on the local, the code works well for me. It seems when we use getJson method to get the JavaScript file, it has the same-origin policy limitations.

    To overcome same-origin policy enforcement when you develop add-ins, you can:

    • Use JSON/P for anonymous access.

    • Implement server-side script using a token-based authentication scheme.

    • Using cross-origin resource sharing (CORS).

    • Build your own proxy using IFRAME and POST MESSAGE.

    You can get more detail about it from link below:
    Addressing same-origin policy limitations in Office Add-ins

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 14, 2015 10:07 AM
    Moderator