Topic
1 reply Latest Post - ‏2013-02-20T09:43:01Z by cloumeifyoudare
cloumeifyoudare
cloumeifyoudare
5 Posts
ACCEPTED ANSWER

Pinned topic VMControl REST API: calling with jQuery:Access-Control-Allow-Orgin

‏2013-02-20T07:40:58Z |
Hi,

Does someone know how to prevent this error when calling VMControl REST API from a jQuery/ajax call:
XMLHttpRequest cannot load https://10.197.210.24:8422/ibm/director/rest/VMControl/hosts. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

How can I change the Systems Director server to allow cross-domain call?
Here is my javascript code:
$('#test').on('click', function () {
//alert("sending request");
$.ajax({
type: 'GET',
url: 'https://10.197.210.24:8422/ibm/director/rest/VMControl/hosts',
contentType: "application/json",
crossDomain: true,
async: false,
dataType: 'json',
//jsonpCallback: 'jsonpCallback',
success: function (response) {
alert('success--' + JSON.stringify(response));
},
error: function (response) {
alert(JSON.stringify(response));
},
beforeSend:
function (xhr) {
xhr.setRequestHeader("Authorization", "Basic "+ $.base64.encode("root:passwd"));
xhr.setRequestHeader("ISDAPIVersion", "6.3.2");
}
});
});
Updated on 2013-02-20T09:43:01Z at 2013-02-20T09:43:01Z by cloumeifyoudare
  • cloumeifyoudare
    cloumeifyoudare
    5 Posts
    ACCEPTED ANSWER

    Re: VMControl REST API: calling with jQuery:Access-Control-Allow-Orgin

    ‏2013-02-20T09:43:01Z  in response to cloumeifyoudare
    Ok, I found a way of doing it successfully:

    $(document).ready(function(){
    $('#test').on('click', function () {
    getdetails("https://root:cloud123@10.197.210.24:8422/ibm/director/rest/VMControl/hosts");
    });
    });
    function getXHR(){
    var xhr=false;
    try {
    xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
    xhr = false;
    }
    }
    if (!xhr && typeof XMLHttpRequest != 'undefined') {
    xhr = new XMLHttpRequest();
    }
    return xhr;
    };

    var getdetails = function(url) {

    var xhr = getXHR();

    var cUri = url;

    cUri = "redir.php?url=" + encodeURIComponent( cUri );
    //xhr.setRequestHeader("Authorization", "Basic "+ $.base64.encode("root:cloud123"));
    //xhr.setRequestHeader("ISDAPIVersion", "6.3.2.0");
    xhr.onreadystatechange= function() {
    if (this.readyState !== 4) return; // not ready yet
    if (this.status == 200) { // HTTP 200 OK
    alert('success--' + this.responseText);
    $('#console').append(this.responseText);
    } else {
    // server returned an error. Do something with it or ignore it
    alert(this.responseText);
    }
    };

    xhr.open( "GET", cUri, false );
    xhr.send();
    };

    and in redir.php:
    <?php
    $url = rwurledecode($_GET);
    $json = file_get_contents($url);
    echo $json;
    ?>

    But if anyone knows how to modify the Systems Director webserver to allow Origin from another server, it would be cool.