1

I've been testing an outgoing REST GET request (towards the other world, not towards SharePoint).

When I'm using this code in pure JavaScript/HTML it works just fine. But when using it in my SharePoint app I'm receiving a JavaScript runtime error:

NetworkError at client.send(null);

This is the code I've tried so far:

var url = "https://test.com/example/something/1";

var client = new XMLHttpRequest();

client.open("GET", url, false);

client.setRequestHeader("Content-Type", "application/json");
client.setRequestHeader("theplatform", "default");
client.setRequestHeader("Authorization", "Basic keyofsomethingvalue%");

client.send(null);     

if (client.status == 200)
    alert("The request succeeded!\n\nThe response representation was:\n\n" + client.responseText)
else
    alert("The request did not succeed!\n\nThe response status was: " + client.status + " " + client.statusText + ".");

Does anybody know what I did wrong?

3
  • This is more of a javascript question than a SharePoint one.
    – Akhoy
    Commented Sep 3, 2015 at 13:23
  • @Akhoy well it is sharepoint related, as when I'm using this code in pure javascript/html it works just fine, but when using sharepoint - nope?
    – Dr Cox
    Commented Sep 3, 2015 at 13:25
  • Right, didn't know that. You should mention that in your question.
    – Akhoy
    Commented Sep 3, 2015 at 13:26

1 Answer 1

4

CSOM and the REST API include the WebProxy for these operations.

Query a remote service using the web proxy in SharePoint 2013

Code sample:

$.ajax({
    url: "../_api/SP.WebProxy.invoke",
    type: "POST",
    data: JSON.stringify(
        {
            "requestInfo": {
                "__metadata": { "type": "SP.WebRequestInfo" },
                "Url": url,
                "Method": "GET",
                "Headers": {
                    "results": [{
                        "__metadata": { "type": "SP.KeyValue" },
                        "Key": "Accept",
                        "Value": "application/json;odata=verbose",
                        "ValueType": "Edm.String"
                    }]
                }
            }
        }),
    headers: {
        "Accept": "application/json;odata=verbose",
        "Content-Type": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: successHandler,
    error: errorHandler
});

Note 1: You need to register the target domain as a remote endpoint in the app manifest.

Note 2: Response size must not exceed 200 Kb.

6
  • This works, I've just got a small problem 'Access to example.com is denied. SharePoint is currently configured to block intranet calls.', do you have any further tips? :)
    – Dr Cox
    Commented Sep 4, 2015 at 7:04
  • Why would you say my next question was a duplicate of this question?
    – Dr Cox
    Commented Sep 4, 2015 at 11:29
  • Now this is my next question.
    – Dr Cox
    Commented Sep 4, 2015 at 11:49
  • Don't update your question like that. I've rolled it back to the previous state so that it matches up with the response. If you are having a problem, open a new question with the specifics. Commented Sep 4, 2015 at 15:42
  • @EricAlexander he had actually opened a new question but it was marked as duplicate. :)
    – Akhoy
    Commented Sep 4, 2015 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.