Scenery
I trying make an a call to an service in IBM Cloud and I have send an file. This request must be done in Sharepoint Online using JavaScript. In the request a file will be sent. Problem I have problem with cross domain(Sharepoint side) and bad request(IBM side) in Sharepoint Online. I tried sereval ways.
Attempts
To solve Cross domain problem
$ .ajax({ url: "../_api/SP.WebProxy.invoke", type: "POST", data: JSON.stringify( { "requestInfo": { "__metadata": { "type": "SP.WebRequestInfo" }, "Url": "https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key={API-KEY}&version=2016-05-20", "Method": "POST", "Headers": { "results": [{ "__metadata": { "type": "SP.KeyValue" }, "Key": "Accept", "Value": "application/json;odata=verbose", "ValueType": "Edm.String" }, { "__metadata": { "type": "SP.KeyValue" }, "Key": "Content-Type", "Value": "application/json", "ValueType": "Edm.String" }] } , "Body": JSON.stringify({ "files":[ { "name":"imagem.png","content": image } ]})//Problem here } }), headers: { "Accept": "application/json;odata=verbose", "Content-Type": "application/json;odata=verbose", "X-RequestDigest": $ ("#__REQUESTDIGEST").val() }, success: function (data) { alert("Sucesso"); console.log(data.d.Invoke.StatusCode); console.log(JSON.stringify(data.d.Invoke.Body)); }, error: function (data) { alert("Falha"); console.log(arguments[2]); } });
This way the problem Cross domain(Sharepoint) is solved, but I receive Bad request(IBM API). The model resquest send file is show bellow.
"Body": JSON.stringify({ "files":[ { "name":"imagem.png","content": image } ]})
To solve Bad request problem:
function CORS() { var data = new FormData(); data.append("imagemTeste", $ ("#imagem")[0].files[0]); var xhr = new XMLHttpRequest(); xhr.open('POST', 'https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key=1c117564efeabecbea9841b49b1aa04b4cc71da6&version=2016-05-20'); xhr.onload = function (e) { var data = JSON.parse(this.response); console.log(data); } xhr.send(data); }
Here the problem Bad request is solved(IBM API), but I get cross domain error(Sharepoint). I’m out of ideas to solve this.