dimanche 30 novembre 2014

problem using JavaScript in SharePoint provider hosted app



I have created a provider hosted app for a SharePoint Online site and want to use JavaScript for most of the front end logic, rather than ASP.NET MVC


I have run into Cross domain problems trying to query REST endpoints from the SharePoint Online domain. this article gives advice on getring a basic example up and running for a JavaScript SharePoint provider hosted app, however it doesn't seem to work for me, as I don't have an SPAppWebUrl paramater in the url...


So I tried just using my app domain as the SPAppWebUrl which is something like http://ift.tt/12iMpc6 but it doesn't seem to work, giving the error


GET 404 http://ift.tt/1vyZOIM (Not Found)


That error makes sense to me, as the test app isn't a SharePoint Online site... If I change it to the SPHostWebUrl, it has the correct endpoint http://ift.tt/12iMpsm... but won't work because of the cross domain restriction, giving the error:


Uncaught SecurityError: Blocked a frame with origin xxxx accessing a frame with origin xxx. Protocols, domains, and ports must match.


Here is my code for a very simple JavaScript app to get some list data. The Endpoint opens fine in the browser when I set it to the SPHostWebUrl but not in the app due to the cross domain error



$(document).ready(function () {
$.getScript(
"Scripts/SP.RequestExecutor.js",
continueExecution
);
});

function continueExecution() {
// have tried both hostweburl and appweburl
executor = new SP.RequestExecutor(hostweburl);
getCustomers();
}

function getCustomers() {

// begin work to call across network
var requestUri = hostweburl + "/_api/Web/Lists/getByTitle('Customers')/items/"
+ "?$select=Id,FirstName,Title";

// execute AJAX request
executor.executeAsync({
url: requestUri,
method: "GET",
headers: { "ACCEPT": "application/json;odata=verbose" },
contentType: "application/json",
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
}







0 commentaires:

Enregistrer un commentaire