Abhishek Gupta
Abhishek Gupta

Reputation: 25

Post data to server using Excel Add-in

The Project is an Excel Web Add-in.

I am currently running the add-in on Visual Studio in debug mode and would like to post data to the server. The data is sent through an ajax call.

Code for Posting data:

 Excel.run(function (context) {
        let workbook = context.workbook;
        let worksheets = workbook.worksheets;
        let Sheet = worksheets.getItem("SheetName");
        var tableRange = Sheet.getRange("Data");
        tableRange.load("values");
        return context.sync().then(function () {
        $.ajax({
                contentType: 'application/json; charset=utf-8',
                dataType: 'text',
                type: 'POST',
                url: 'DocFile/Post',
                crossDomain: true,
                data: JSON.stringify({
                    file:tableRange.values
                })
                ,
                success: function (r) {
                    console.log(r);
                    alert("Successfully saved Home office assumptions for Space");
                },
                failure: function (r) {
                    alert(r);
                }
            });

The following error message pops up when running the add-in: Error Message

Upvotes: 1

Views: 415

Answers (1)

Raymond Lu
Raymond Lu

Reputation: 2236

From this error message looks like you are posting data to the Cross-domain server. in Excel JS solution, we have the same-origin policy enforced by the browser prevents a script loaded from one domain from getting or manipulating properties of a webpage from another domain. This means that, by default, the domain of a requested URL must be the same as the domain of the current webpage.

The document can be found at https://learn.microsoft.com/en-us/office/dev/add-ins/develop/addressing-same-origin-policy-limitations

A possible solution could be to add this new appdomain into the manifest, you could find the document at here https://learn.microsoft.com/en-us/office/dev/add-ins/reference/manifest/appdomains

Upvotes: 1

Related Questions