IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Services Web Discussion :

Comment acceder a un webservice via du javascript ?


Sujet :

Services Web

  1. #1
    Membre confirmé
    Inscrit en
    Mai 2004
    Messages
    92
    Détails du profil
    Informations forums :
    Inscription : Mai 2004
    Messages : 92
    Par défaut Comment acceder a un webservice via du javascript ?
    Salut,

    Je cherche un moyen a travers une page html d utiliser un web service (c#) grace a du code javascript.

    voila ce que j ai code pour acceder a la methode Helloworld du template par defaut de visual studio 2005 pour les webservices. Neanmoins cela ne marche pas. Sauriez vous ce qui cloche ? ou s il y a un meuilleur moyen d acceder a un webservice via du javascript dans une page html ?

    Merci d avance pour votre aide.
    Arioule.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
     
     
    <script type="text/javascript" language="javascript"> 
     
     // variables
     var strMethodName;
     var strSoapContent; 
     
     var soapAction;
     var url;
     var data;
     
     var xmlhttp ;
     
     
     
     
     //var strSoapContent;
    //Function to create parameter for WebService method.function 
        function createWSParam()
        {
            strMethodName = 'HelloWorld' ;
            //var strSoapContent = '<countryName>' + count4WSCall + '</ countryName >\n' +  '<boolAsync>' + strCountryName + '</ boolAsync >\n';
            strSoapContent = "" ;   
         }
     
         //Function to create SOAP data packet.function
         function createSOAPData(strMethodName, strSoapContent)
         {
             soapAction = 'http://tempuri.org/' + strMethodName ;
             url = 'http://localhost:1329/MonService/MonService.asmx' ;
             data = '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n'
             +'<soap:Envelope xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-                        instance\' xmlns:xsd=\'http://www.w3.org/2001/XMLSchema\' xmlns:soap=\'http://schemas.xmlsoap.org/soap/envelope/\'>\n'
             + '<soap:Body>\n'
             + '<' + strMethodName + ' xmlns=\'http://tempuri.org/\'>\n'
             + strSoapContent
             + '</' + strMethodName + '>\n'
             + '</soap:Body>\n'
             + '</soap:Envelope>';
         }
     
         //Function to create XMLHttpRequest.function
         function getXmlHttpObject()
         {
            if(window.ActiveXObject)
            {
                try
                {
                    xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
                }
                catch (ex)
                {
                    xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
                }
             }
             else if (window.XMLHttpRequest)    //For Non-IE
             {
                xmlhttp = new XMLHttpRequest();
             }
         }
     
         //Function to send the SOAP Request.function
         function sendSOAPDataToWebService(xmlhttp, url, data, soapAction)
         {
            xmlhttp.open('POST', url, true);
            xmlhttp.onreadystatechange = processgetSOAPResponseData ;
            xmlhttp.setRequestHeader('Content-Type', 'text/xml');
            xmlhttp.setRequestHeader('SOAPAction', soapAction);
            xmlhttp.send(data);
         }
     
         //Function to process the SOAP Response.function 
         function processgetSOAPResponseData()
         {
             if (xmlhttp.readyState == 4)
             {
                 if (xmlhttp.status == 200) // only if "OK"
                 {
                    try
                    {
                        if (window.ActiveXObject)   // code for IE
                        {
                            doc = new ActiveXObject("Microsoft.XMLDOM");
                            doc.async="false";
                            doc.loadXML(xmlhttp.responseText);
                        }
                        else    // code for Mozilla, Firefox, Opera, etc.
                        {
                            var parser=new DOMParser();
                            doc = parser.parseFromString(xmlhttp.responseText,"text/xml");
                        }
                        var rootNode = doc.documentElement;                    // documentElement always represents the root node
                        alert("message : " + rootNode)
                        /*
                        if( rootNode.childNodes[0].childNodes[0].childNodes[0].nodeName == "getCurrentPositionDataResult" )
                        {
                            //Method to process the SOAP data
                            getCurrentPositionData(rootNode) ;
                        }
                        */
                     }
                     catch (ex){}
                  }
              }
          }
     
          // main
     
          function init()
          {
              createWSParam();
              createSOAPData(strMethodName, strSoapContent);
              getXmlHttpObject();
              sendSOAPDataToWebService(xmlhttp, url, data, soapAction);
          }       
     
    </script> 
     
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
    </head>
    <body onload="init()">
    <form id="form1" runat="server"> 
    <div> 
    </div> 
    </form> 
    </body>
    </html>

  2. #2
    Membre expérimenté
    Avatar de Dung-Tri
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    109
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : Belgique

    Informations forums :
    Inscription : Avril 2007
    Messages : 109
    Par défaut Solution
    Regarde ce post, ca marche nikel : http://www.developpez.net/forums/d54...ce-javascript/


    Cdt.

  3. #3
    Membre confirmé
    Inscrit en
    Mai 2004
    Messages
    92
    Détails du profil
    Informations forums :
    Inscription : Mai 2004
    Messages : 92
    Par défaut Comment acceder a un webservice via du javascript ?
    Merci de ton aide.
    Cordialement.
    Arioule

Discussions similaires

  1. Réponses: 1
    Dernier message: 10/06/2008, 23h30
  2. Réponses: 4
    Dernier message: 14/02/2007, 10h37
  3. Réponses: 4
    Dernier message: 20/10/2006, 16h19
  4. [POO] Comment acceder a un objet javascript instancie dans une page mere
    Par herbert dans le forum Général JavaScript
    Réponses: 11
    Dernier message: 20/07/2006, 19h34
  5. Réponses: 7
    Dernier message: 11/01/2006, 16h55

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo