Tyler Wozniak
Tyler Wozniak

Reputation: 11

External Ajax Call in Sharepoint 2013: Access is Denied

So, I am trying to make an extremely simple sharepoint application. I just want an already built and hosted web application to be loaded within the sharepoint page header.

The goal thus was to simply load in the Html from the alternate page and place it in a div on the Sharepoint application's Default.aspx. This is the same way we've pulled in another external project into a non-sharepoint web application with absolutely no difficulties.

So, I have made the following ajax call:

$.ajax(
        {
            type: "GET",
            url: "http://PageIWantToLoad/default.aspx",
            dataType: "html",
            success: function (result) {
                $("#pageContainer").html(result);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert("oops");
            }

    });

However, I get an Access is Denied error.

I've checked a few similar StackOverflow and other online help requests, but all of them seemed to be dealing with more complicated systems/situations.

Any help would be appreciated.

Upvotes: 1

Views: 6769

Answers (1)

Nikolay Zainchkovskiy
Nikolay Zainchkovskiy

Reputation: 474

Consider this article: http://msdn.microsoft.com/en-us/library/jj164022.aspx. You need use Sharepoint cross-domain library, if you want make javascript request from SharePoint. There is my example of requesting SharePoint 2013 REST Service from SharePoint itself. I'm used Script on Demand library and mQuery (SharePoint built-in analog of jQuery). And SP.RequestExecutor - this is cross-domain SharePoint library.

SP.SOD.executeFunc('mQuery.js', 'm$', function() {
m$.ready(function() { 
    SP.SOD.registerSod('sp.requestexecutor.js',
        '/_layouts/15/sp.requestexecutor.js');
    SP.SOD.executeFunc('sp.requestexecutor.js', 
        'SP.RequestExecutor', 
    function() {
        var targetSiteUrl = "http://mySiteUrl";
    var targetUrl = "http://mySiteUrl/_api/web/lists/getByTitle('myListTitle')/items(1)";
var re = new SP.RequestExecutor(targetSiteUrl);
re.executeAsync({
    url: targetUrl,
    headers: { "Accept": "application/json; odata=verbose" },
    method: 'GET',
    success:function(response) {
        var jsonObject = JSON.parse(response.body);
            }
        });
    });
})});

The key is RequestExecutor. If you want to request to SharePoint from external resource - you will need accessToken. Hope this helps.

Upvotes: 2

Related Questions