Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest way you can call a webservice in JavaScript without Ajax or anything else like that? [duplicate]

Possible Duplicate:
Consuming a Web service using Javascript

Please note that I'm still getting used to JavaScript. Basically I need to write an HTML file that uses Javascript to call a couple of web service methods (from the same server as the HTML file) without using Ajax or probably much of anything else that we'd have to install on there separately. We already have the web service up and running fine, as well as some JS. This needs to work for IE, FF, and Chrome, including for computers that are a couple of years "behind the times". What is a really simple way to do this? Again, I'm still kind of getting my feet wet with JavaScript, so I'm having a hard time following and using some of the examples I've seen. Thanks!

EDIT: Here's an example of the kind of thing I've been trying:

<html>
<head>
   <title>Hello World</title>
   <script language="JavaScript">
      var iCallID;
      function InitializeService(){
     alert("spam");
     try {
            service.useService(<WSDL address>, 
                  "HelloWorldService");
     } catch (e) {
        alert(e.message);
     }
     alert("spam");
         service.HelloWorldService.callService("HelloWorld");
     alert("spam");
      }
      function ShowResult(){
     alert("spam");
         alert(event.result.value);
     alert("spam");
      }
</script>

I get "spam", then "Object doesn't support this property or method", then nothing.

like image 770
Panzercrisis Avatar asked Dec 03 '25 12:12

Panzercrisis


1 Answers

Ok, AJAX has been around since donkey's years, really. But since you're not familiar with JS, here's a little copy-paste function you could use:

function ajax(url,method,data)
{
    var xhr;//ajax object
    data = data || {};//default ajax request
    method = method || 'POST';
    url = url || 'default/url/ajax';
    try
    {//normal browsers
        ret = new XMLHttpRequest();
    }
    catch (error)
    {//older IE browsers, including dead IE6
        try
        {
            ret= new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch(error)
        {
            try
            {
                ret= new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch(error)
            {
                throw new Error('no Ajax support?');
            }
        }
    }
    if (typeof ret !== 'object')
    {//if fails (almost impossible)
        throw new Error('No Ajax, FFS');
    }
    ret.open(method, url, true);//open ajax connection
    ret.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
    ret.setRequestHeader('Content-type', 'application/x-www-form-urlencode');
    ret.onreadystatechange = function()
    {
        if (this.readyState === 4 && this.status === 200)
        {
            var response = this.responseText;
            //code you want to see executed when the call was successful
        }
    };
    return ret.send(data);
}

Usage:

ajax('your/ajax/url','POST','id=12');

This will send an ajax request back to the server, with a POST parameter id, value 12... play around with that. A few things that can be useful: JSON, and -though I'm not a big fan- jQuery, it has a ready built-in $.ajax method, just pass an object literal and you're good to go.

Feel free to edit this function as you please

like image 69
Elias Van Ootegem Avatar answered Dec 06 '25 00:12

Elias Van Ootegem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!