hsidhu Posted February 26, 2016 Report Posted February 26, 2016 Hello Everyone, I am trying to make javascript ajax call to rest api of voida api e.g /rest/session but getting cors error. I tried different solutions but didn't worked. I am using following code. var vars = { "name": "auth", "value": "admin " + CryptoJS.MD5('password') }; $.ajax({ type: "PUT", url: 'http://[pbx]/rest/system/session', headers:{ "Access-Control-Allow-Origin": "*", 'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With, Origin, Accept', 'Access-Control-Allow-Methods': 'HEAD, GET, POST, PUT, DELETE, OPTIONS', }, dataType: 'json', async: true, data: JSON.stringify(vars), success: function(data){ console.log(data); }, error: function(data){ console.log(data); } }); I am getting following errors OPTIONS http://[pbx]/rest/system/session send @ jquery.js:8630jQuery.extend.ajax @ jquery.js:8166loadView @ status.js:396(anonymous function) XMLHttpRequest cannot load http://[pbx]/rest/system/session. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://[HOST]' is therefore not allowed access. The response had HTTP status code 403. I also listed ip under access IP control IF you guys can look into it will solve another problems for me. Quote
Vodia Telephone System Posted February 29, 2016 Report Posted February 29, 2016 Are you loading the javascript file from one server and the pbx is on another server? If so, that won't work because of different origins. These queries can only be sent to the server which provided the javascipt file. Thanks. Quote
hsidhu Posted February 29, 2016 Author Report Posted February 29, 2016 Yeah, I am sending that query from different origin. Is there any work around that if i wanna to send this from different server. The purpose of doing this is also related with setting up a websocket connection from different origin. Bcz its not possible to send custom header with javascript websocket client. Authorization and websockets headers Quote
Vodia Telephone System Posted February 29, 2016 Report Posted February 29, 2016 You could do it from the server itself and present it to the browser. So anything the browser wants to do with the PBX, it goes through your server (where it's the server that's talking to the PBX) and then return the value if any to the browser. Thanks. Quote
hsidhu Posted February 29, 2016 Author Report Posted February 29, 2016 Yeah, I tested that it works fine but the thing is than you have to go through few extra steps. On the side not it only works with session cookie not with basic authorization. Best Regards Quote
hsidhu Posted March 1, 2016 Author Report Posted March 1, 2016 Here is an example of websocket client call base on Vodia rest api with few modifications.update index.php file with you pbx server host/ip and extension username and passward Quote
hsidhu Posted March 1, 2016 Author Report Posted March 1, 2016 Here is an example of websocket client call base on Vodia rest api with few modifications. update index.php file with you pbx server host/ip and extension username and passward websocketClient.zip Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.