Wolfchamane
Wolfchamane

Reputation: 255

SOAP request through jQuery AJAX

I have a server running a service, I want to run at some interval a ping request to the service so I can know when it's ready or not.

Got the following ping.dat file:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                  xmlns:dvt="[private]">
    <soapenv:Header />
    <soapenv:Body>
        <dvt:Ping/>
    </soapenv:Body>
</soapenv:Envelope>

And the following Javascript functions (which will be included into the setInterval() function):

function doAjax() {     
    //load request document
    $.ajax({
        cache: false,
        crossDomain: true,
        async: true,                
        dataType: 'xml',
        type: 'POST',
        data: null,
        url: "./ping.dat",
        error: function(xhr, sta, err){ alert(err); },
        success: function(ret, sta, xhr){
            //ping service
            $.ajax({
                cache: false,
                crossDomain: true,
                async: false,
                processData: false,
                contentType: "text/xml; charset=\"UTF-8\"",
                dataType: 'xml',
                data: processXML(xhr.responseText),
                type: 'POST', 
                url: "[private]",
                error: function(xhr, sta, err){ 
                    alert(err); 
                },
                success: function(ret, sta, xhr){ 
                    $('#response').text($.trim(ret)); 
                },
                complete: function(xhr, sta){
                    alert('complete');
                },
            });
        }
    });
}

function processXML(text){
    var ret = null;
    if ((typeof(ret) !== 'undefined')&&(text !== null)&&(text.length !== 0))
        ret = $.trim(text.replace(/[\n\t]+/g, ''));
    
    return ret;
}

When I use SoapUI to call the service and load the ping request, it works.

When I use the JS functions, browser reports:

OPTIONS [private] 200 (OK) jquery-1.10.2.js:8706

XMLHttpRequest cannot load [private]. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

What's causing this?

Upvotes: 0

Views: 2151

Answers (1)

Bogdan
Bogdan

Reputation: 24590

The message is clear: No 'Access-Control-Allow-Origin' header is present on the requested resource.

If you are indeed doing a cross domain Ajax request, then the server must respond with the appropriate HTTP headers. Browser issues an OPTIONS HTTP request and checks to see if the server "approves" access by looking at the received headers. If the headers are missing then the browser is obligated to return an error and disallow the request to the resource.

See here for details: HTTP access control (CORS)

SoapUI is not affected by the same origin security policy like a browser is, so that's why pinging the web service from SoapUI works.

Upvotes: 1

Related Questions