0

How can I make get request from Sharepoint object model to external web service? I tried did it with ajax request:

 $.get("http:/servername/webservice_name", function(data, status) {
alert("Data: " + data[0] + "\nStatus: " + status);

});

But I have an error: Access denied. My web service has response in JSON format. I have read a lot of article, but there are no decisions. I read, that ajax forbidden in SharePoint for external web service. How can I make it without ajax?

Dim
  • 532
  • 1
  • 6
  • 23
  • Sharepoint version? Is the service using the same domain? – Jeroen Heier Jul 24 '16 at 08:33
  • Sharepoint 2013 online. No, web service has other domain – Dim Jul 24 '16 at 08:57
  • Access denied. means user do not have permissions in the external web site – bresleveloper Jul 24 '16 at 10:02
  • No, If I copy this script to the other html page (without Sharepoint) it is work correct, without errors. – Dim Jul 24 '16 at 10:05
  • Is the "other" html page also on a webserver? Maybe the security setting are others. For example when you have set in the iis authentication to windows or anynomous or ... and this is different in SharePoint. In one case it can use the AppPool Account in SharePoint the User context. – STORM Jul 24 '16 at 10:44
  • No, no, no. It is different web servers and different domain. And I think ajax get requests forbidden to external web services. In Sharepoint online it is doesn't working and in the Postman or other programs, or usual html page it is working without authentication and any problems. And I need to use maybe mQuery, or other libraries for cross domain requests. But there are no examples how to use it. – Dim Jul 24 '16 at 11:20

2 Answers2

0

If you believe that the use of AJAX or a specific library is preventing you from accessing the web service, you can try invoking the web service directly using a native JavaScript XMLHttpRequest.

For example:

var verb = "GET";
var url = "http://servername/webservice_name";
var xhr = new XMLHttpRequest();
xhr.open(verb, url, true);
xhr.setRequestHeader("Content-Type","application/json");
xhr.onreadystatechange = function(){
    if(xhr.readyState == 4){ 
        myCallbackFunction(xhr.status, xhr.responseText);
    }
};
xhr.send(data);

function myCallbackFunction(status, text){
    // do something with the results based on the status
}

You should also confirm that your Internet Explorer settings are the same for SharePoint as they are for the HTML page where you're able to get the web service to work. Specifically, you'll want to check the browser mode and security settings.

Confirm that the problem still exists when the settings are identical before trying to troubleshoot your network or code.

Thriggle
  • 7,009
  • 2
  • 26
  • 37
0

In the SharePoint online it is work only for https web services. http from https web sites not allowed. Final code, which work with https:

$(document).ready(function(){
$.ajax({
    url: "https://services.odata.org/Northwind/Northwind.svc/Customers",
    type: "GET",
    headers: { "ACCEPT": "application/json;odata=verbose" },
    async: false,
    success: function (data) {
        if(data.d.results.length>0){
            alert("Results Count:"+data.d.results.length);
        }else{
            alert("no data");
        }
    },
    error: function () {
        //alert("Failed to get details");                
    }
});

});

Dim
  • 532
  • 1
  • 6
  • 23