Ajax Post Error on Office Add-in from Outlook 2013 - ajax

I have simple requirement here to fetch data from Exchange Online EWS API & SharePoint lists and display those data onto HTML page of Apps for Office. This office app should work on OWA (Web mail) as well as Outlook client. I was able to see the response data in browser (OWA - Web mail), but when I am trying to load the same add-in on outlook client it doesn't show the data and gives the error in errorMessage as:
No Transport
So, we did try enabling the CORS using $.support.cors = true as per this thread, but no luck with that and got Access Denied error.
Does anyone have found the solution for this kind of scenario. Any help would be much appreciated.
Below is code snippet where I am getting that error:
$.ajax({
url: "https://myazuretenant.azurewebsites.net/GetData.asmx/GetLocations",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
// Data Manipulation goes here
},
error: function (data, errorCode, errorMessage) {
app.showNotification('Error: ' + errorCode + ' - ' + errorMessage);
}
});
Note: Ajax request is going through the generic Web API (ASMX Service), which fetches the data using ExchangeService library (EWS API) and SharePoint.

Related

How to call Bamboo HR API call from SharePoint Online

I would like to get data from Bamboo HR and display it in SharePoint Online. Bamboo HR supports API with basic authentication to call and get data from Bamboo HR. When I tried to get data in browser, it's working fine however when I use ajax call inside Script Editor Webpart in SharePoint, it gives me cross origin error. I also tried with jsonp using $.getJSON however it's also giving an error as below:
function test(a,b,c){debugger;
console.log(a);
}
$.ajax({
url:"https://api.bamboohr.com/api/gateway.php/test/v1/time_off/requests/?start=2018-04-25&end=2018-04-25&status=approved",
type: "GET",
headers: {
"accept": "application/json",
'Authorization': "Basic api_key:x")
},
dataType: 'jsonp',
jsonpCallback: 'test',
});
Above code throwing below error in SharePoint Online environment.
Refused to execute script from
'https://api.bamboohr.com/api/gateway.php/test/v1/time_off/requests/?start=2018-04-25&end=2018-04-25&status=approved&callback=test&_=1524995081404'
because its MIME type ('text/xml') is not executable, and strict MIME
type checking is enabled.
Any help to retrieve data from Bamboo HR into SharePoint will be appreciated.
Thanks in advance.
Here are two ways for your reference:
1.Pushing data to SharePoint list using RESTful service.
Create a job and push the data from Bamboo HR to SharePoint List using REST API of CSOM(C#).
2.Using Business Connectivity Service(BCS).
Manage Business Connectivity Service Applications

Send POST AJAX request from Office Add-In

I'm trying to send POST Ajax request for third party service from my Outlook Add-in, but no matter what I tried I receiving Error: Access is denied, and status 0 (request never hit the server).
Assuming we are running IE9 or 8 behind the outlook I tried old school hacks like https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest.
$.ajax({
url: endpoint,
data: JSON.stringify({'1':'2'}),
// headers: {'X-Requested-With': 'XMLHttpRequest'},
contentType: 'text/plain',
type: 'POST',
dataType: 'json',
error: function(xhr, status, error) {
// error
}
}).done(function(data) {
// done
});
Is there is something more I need to implement? Of cause I add my domain to manifest AppDomain property.
Cheers
The following needs to be done to send request to 3rd party service ...
Add the service URI to AppDomain list (you've done it.)
The service MUST have SSL endpoint; "https://your.domain" must be included within of "AppDomain" entry (see above)
The service has to allow CORS requests for your application (hosted Outlook App URI) domain or any domain. This is up to the service creators to allow or disallow client apps connections via Ajax.
As of observation of your code I notices you are sending JSON object, but setting content type to "text/plain". Contact the service creators to get information on what type of the data they accept as request. Usually services allow "application/json", but not plain text.

Access response data in JSONP request

I have a SAP Gateway OData-Service and a local Tomcat Apache Server. My SAPUI5 Client is deployed in the Tomcat and requests a OData-Webservice from the SAP Gateway remote server. In fact there is a cross origin domain error. So I set the header "Access-Control-Allow-Origin" in my OData-Webservice and my SAPUI5 client requests with JSONP, but I will get an error because the SAP Gateway can't handle with JSONP responses.
My code:
The error: "Uncaught SyntaxError: Unexpected token :"
The error depends on the incompatibility of the SAP Gateway to JSONP.
If I look in the network requests I will find this one:
It is the JSON (not JSONP) response from the webservice.
So my question. Is there a possibility to access to this response?
I tried to access via a lot of callbacks like success, error, fail, done, always, complete, and so on. But no chance...
thanks and best regards
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'jsonCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function(response) {
//this is where you'll get data/response
console.log(response) ;
},
error: function(e) {
console.log(e.message);
}
});
Update:
Problem is with your context.. there is some value like _1453458442107=:1
Check that and encode it.
If you set the CORS header "Access-Control-Allow-Origin" in the odata responses you should be able to access the service with regular JSON (without the P).
JSONP is a workaround to circumvent the same origin policy when the CORS-Headers are not available.
If your odata service uses authentification it might be necessary to set some more CORS headers to get it working.
On MDN you can read more about CORS and its headers.
On Wikipedia is a chapter about CORS vs JSONP.
So have you tried something like the following?
$.get({
url:"https://.../sap/opu/odata/sap/ZMOBILAD_SRV/UsernameSet?$format=json",
context: document.body,
cache: false
}).done(function(){
console.log(this);
});

phonegap ajax request error over ssl

I'm trying to post request over SSL in my Phonegap application dedicated especially for Windows Phone platform. Everything works on our development server, but when I switched to production, my AJAX request failed. Below I paste my AJAX call. I've tested my application on Android, iOS and Windows Phone device. It doesn't work only on Windows Phone. Any helper logs from xhr object are empty... I can't figure out what's wrong. Is this problem might be releated because of certificate we use on our production server? Or there are any restrictions that prevent call AJAX request over SSL in Windows Phone? I even don't know how to start debug because all logs are empty... Your help would be greatly appreciated!
Here is my code
$.ajax({
url: myServiceUrl,
type: "POST",
data: myXmlData,
dataType: "xml",
contentType: "application/soap+xml; charset=utf-8",
success: function (data, status, xhr) {
console.log(status);
console.log(xhr.responseText);
onSuccess(data, status, xhr);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log("error");
console.log(xhr.statusText);
console.log(xhr.responseText);
console.log(thrownError);
onError(xhr, ajaxOptions, thrownError);
}
});
My Logs
Log:["error","DebugConsole517333971"]
The thread 0xe4c has exited with code 259 (0x103).
Log:["error","DebugConsole517333972"]
The thread 0xed4 has exited with code 259 (0x103).
Log:["","DebugConsole517333973"]
The thread 0x360 has exited with code 259 (0x103).
Log:["","DebugConsole517333974"]
After long days of debugging I've found soulution... It was cross-domain request of course and if you want to execute POST request over SSL your dataType must be jsonp. It's written in documentation http://api.jquery.com/jQuery.ajax under crossDomain setting. My communication was based on SOAP protocol (xml data) that prevent to execute any request. If I post request on server without SSL protection (still SOAP), it also works. Probably jQuery treated my request as unsafe. I hope this might help someone.

Why does my web service call fail when calling a different site

I am making a JQuery ajax call to a web service like so:
$.ajax({
type: "POST",
url: "https://WebsiteName.com/Service.asmx/LoginExternal",
data: "{loginData: " + JSON.stringify(LoginData) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
... Stuff ...
},
error: {
... Error Alert ...
}
});
When I am on the actual web site, this succeeds. When I am on localhost and use a relative path, it succeeds. However, when I am trying to access the web site (using an absolute path) from localhost, I get the error message that the call failed.
This would seem to indicate that it is a permissions problem and I've made sure that the web.config doesn't require authentication for access to the web service but I'm not sure what else to check. Any help would be greatly appreciated!
Update: The "thrownError" that I am getting is "No Transport" - hopefully that helps.
Security:
http://en.wikipedia.org/wiki/Same_origin_policy
Being an unsecured scripting logic, ajax requests made using JavaScript (or JQuery) are stucked on same host, protocol and port, due to unauthenticated and unencrypted requests.
A method for passing requests to a different host, you must implement a bridge service written in a secure language (ex. Java), deployed on same host that passes your requests further to your services, and then passes response to your ajax call.
Your ajax call will be on the bridge service then.

Resources