Skip to main content
7 of 7
replaced http://stackoverflow.com/ with https://stackoverflow.com/

I solved it like this, when I had the same problem, I used SP.WebProxy for my cross-domain call. I'm actually using it right now.

When calling a "http" from a "https" which sharepoint is, you can use SP.WebProxy as a middle man, see here

And don't forget to specify remote endpoints in the Appmanifest.xml

It looks like this in my default.aspx:

Example GET

<!-- Placeholder for the remote content -->
<span id="categories"></span>

<!-- Add references to the JavaScript libraries. -->
<script 
    type="text/javascript" 
    src="../_layouts/15/SP.Runtime.js">
</script>
<script 
    type="text/javascript" 
    src="../_layouts/15/SP.js">
</script>
<script type="text/javascript">

        (function () {
        "use strict";
        var examplevariable1 = "";
        var examplevariable2 = "";
        var examplevariable3 = "";

        var context = SP.ClientContext.get_current();
        var request = new SP.WebRequestInfo();
        request.set_url("example/api/savingsaccounts/1");
        request.set_method("GET");
        request.set_headers({ "Accept": "application/json;odata=verbose" });

        var response = SP.WebProxy.invoke(context, request);
        context.executeQueryAsync(successHandler, errorHandler);

        function successHandler(data) {
            if (response.get_statusCode() == 200) {
                var json = JSON.parse(response.get_body());
                var output = "<UL>";
                for (var key in json) {
                    if (json.hasOwnProperty(key)) {
                        examplevariable1 = (json[key].id); //ExampleJSONObject
                        examplevariable2 = (json[key].ExampleJSONObject);
                        examplevariable3 = (json[key].ExampleJSONObject);
                        call_a_method (examplevariable1, examplevariable2, examplevariable3);
                        document.getElementById("categories").innerHTML = examplevariable3; //example output
                    }
                }

            }
            else {
                var errordesc;
                errordesc = "<P>Status code: " +
                     response.get_statusCode() + "<br/>";
                errordesc += response.get_body();
                document.getElementById("categories").innerHTML = errordesc;
            }
        }

        function errorHandler() {
            document.getElementById("categories").innerHTML =
                response.get_body();
        }
    })();
</script>

Example PUT

ClosedId = some id i got from some other onclick

function RESTPUTCLOSED(c) {
    var dataObjectClosed = { 'status': 'Closed' }
    var context = SP.ClientContext.get_current();
    var request = new SP.WebRequestInfo();
    request.set_url("http://example/REST/_api/savingsaccounts/" + ClosedId);
    request.set_method("PUT");
    request.set_headers({ "Content-Type": "application/json; charset=utf-8" });
    request.set_body(JSON.stringify(dataObjectClosed));
    var response = SP.WebProxy.invoke(context, request);
    context.executeQueryAsync(successHandler, errorHandler);
}

If you need to authorize ( log in / confirm your access to the remote service)

add this:

request.set_headers({ "Authorization": "somekey_ASdkjsakdjaKSJDlksd" });

To generate the authorization key, follow Shauna's answer here

Dr Cox
  • 445
  • 1
  • 4
  • 22