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

JavaScript Discussion :

Consommation Web service


Sujet :

JavaScript

  1. #1
    Débutant  
    Inscrit en
    Mai 2006
    Messages
    705
    Détails du profil
    Informations forums :
    Inscription : Mai 2006
    Messages : 705
    Points : 117
    Points
    117
    Par défaut Consommation Web service
    Bonjour à toutes et à tous,

    je cherche à avoir un exemple de consommation d'un web service via Javascript.

    Voiçi le lien d'un web service publique et gratuit:

    http://www.webservicex.net/CurrencyConvertor.asmx?WSDL

    Le problème ici c'est que j'ai cherché sur internet et j'ai pas trouvé beaucoup de résultats sur comment consommer un web service en passant des arguments/paramètres (comme c'est le cas pour le web service cité ci-dessus: FromCurrency / ToCurrency).

    J'ai essayé avec ce code, mais rien n'est affiché:

    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
     
    <script type="text/javascript">
           function soap() {
     
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.open('POST', 'http://www.webservicex.net/CurrencyConvertor.asmx', true);
     
                // build SOAP request
                var sr =
                    '<?xml version="1.0" encoding="utf-8"?>' +
                    '<soapenv:Envelope ' + 
                        'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
                        'xmlns:api="http://127.0.0.1/Integrics/Enswitch/API" ' +
                        'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
                        'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">' +
                        '<soapenv:Body>' +
                            '<api:some_api_call soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' +
                                '<FromCurrency xsi:type="xsd:string">TND</FromCurrency>' +
                                '<ToCurrency xsi:type="xsd:string">EUR</ToCurrency>' +
                            '</api:some_api_call>' +
                        '</soapenv:Body>' +
                    '</soapenv:Envelope>';
     
                xmlhttp.onreadystatechange = function () {
     
                    if (xmlhttp.readyState == 4) {
                        if (xmlhttp.status == 200) {
     
                            alert('done use firebug to see response');
                        }
                    }
                    if (xmlhttp.readyState == 0) {
                        alert('open() has not been called yet');
                    }
     
                    if (xmlhttp.readyState == 1) {
                        alert('send() has not been called yet');
                    }
     
                    if (xmlhttp.readyState == 2) {
                        alert('send() has been called, ...');
                    }
     
                    if (xmlhttp.readyState == 3) {
                        alert('Interactive - Downloading');
                    }
                    else
                    alert('Consuming Error');
                }
                // Send the POST request
                xmlhttp.setRequestHeader('Content-Type', 'text/xml');
                xmlhttp.send(sr);
                // send request
                // ...
            }
        </script>
    NB: J'invoque la méthode post() lorsque j'appuie sur un bouton d'un formulaire.

    Comment pourrais-je règler ça?

    Merci

  2. #2
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 640
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 640
    Points : 66 665
    Points
    66 665
    Billets dans le blog
    1
    Par défaut
    NB: J'invoque la méthode post() lorsque j'appuie sur un bouton d'un formulaire.
    un bouton de type sumbit ?
    as tu inhibé l'envoi du form ?

  3. #3
    Débutant  
    Inscrit en
    Mai 2006
    Messages
    705
    Détails du profil
    Informations forums :
    Inscription : Mai 2006
    Messages : 705
    Points : 117
    Points
    117
    Par défaut
    Oui, c'est un bouton de type submit.

    as tu inhibé l'envoi du form ?
    Voila:
    <form action="#">

  4. #4
    Débutant  
    Inscrit en
    Mai 2006
    Messages
    705
    Détails du profil
    Informations forums :
    Inscription : Mai 2006
    Messages : 705
    Points : 117
    Points
    117
    Par défaut
    Juste comme remarque:

    J'ai mis une alert au niveau de la méthode xmlhttp.onreadystatechange mais elle ne s'affiche pas, donc cette méthode n'est pas entrain d'être invoquée !

  5. #5
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 640
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 640
    Points : 66 665
    Points
    66 665
    Billets dans le blog
    1
    Par défaut
    tu appelles ça inhiber l'envoi ...

    ton form est envoyé ... tu perds ton ajax !

    change ton bouton en input type button
    ou mets un return false dans le onsubmit
    là ton envoi sera inhibé ...

  6. #6
    Débutant  
    Inscrit en
    Mai 2006
    Messages
    705
    Détails du profil
    Informations forums :
    Inscription : Mai 2006
    Messages : 705
    Points : 117
    Points
    117
    Par défaut
    J'ai enlevé le "#" et j'ai changé le type du bouton en button mais ça ne fonctionne toujours pas.... rien n'est affiché!

  7. #7
    Débutant  
    Inscrit en
    Mai 2006
    Messages
    705
    Détails du profil
    Informations forums :
    Inscription : Mai 2006
    Messages : 705
    Points : 117
    Points
    117
    Par défaut
    Je ne suis pas trop expert dans le domaine du web service, mais je me demande si la faute provient de la chaine sr ? car j'ai un peu changé y dedans...

  8. #8
    Débutant  
    Inscrit en
    Mai 2006
    Messages
    705
    Détails du profil
    Informations forums :
    Inscription : Mai 2006
    Messages : 705
    Points : 117
    Points
    117
    Par défaut
    Voici comment j'ai changé l'entête:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    var sr =
               '<?xml version="1.0" encoding="utf-8"?>' +
               '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET/">'+
       		   '<soapenv:Header/>'+
       			'<soapenv:Body>'+
          		'<web:ConversionRate>'+
             '<web:FromCurrency>EUR</web:FromCurrency>'+
             '<web:ToCurrency>TND</web:ToCurrency>'+
          		'</web:ConversionRate>'+
       			'</soapenv:Body>'+
    			'</soapenv:Envelope>';
    Mais malheureusement, ca ne marche toujours pas

    Remarque: cette entête a été générée avec le SoapUI.

  9. #9
    Modérateur

    Avatar de NoSmoking
    Homme Profil pro
    Inscrit en
    Janvier 2011
    Messages
    17 085
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2011
    Messages : 17 085
    Points : 44 682
    Points
    44 682
    Par défaut
    Bonjour,
    pourrais tu montrer le code HTML de ton formulaire?

  10. #10
    Débutant  
    Inscrit en
    Mai 2006
    Messages
    705
    Détails du profil
    Informations forums :
    Inscription : Mai 2006
    Messages : 705
    Points : 117
    Points
    117
    Par défaut
    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
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    <!DOCTYPE html>
    <html>
     
    <head>
        <meta charset="utf-8"/>
        <meta name="description" content="A Tizen Web UI FW multi-page template generated by Tizen Web IDE"/>
        <meta name="viewport" content="width=device-width,user-scalable=no"/>
     
        <link rel="stylesheet" href="tizen-web-ui-fw/latest/themes/tizen-white/tizen-web-ui-fw-theme.css" name="tizen-theme"/>
        <title>Tizen Web IDE - Template - Tizen - Tizen Web UI Framework - Mutil-Page</title>
        <!--NOTE:
            jquery.js and web-ui-fw.js must be included.
            DO NOT REMOVE below code!
        -->
        <script src="tizen-web-ui-fw/latest/js/jquery.js"></script>
        <script src="tizen-web-ui-fw/latest/js/tizen-web-ui-fw-libs.js"></script>
        <script src="tizen-web-ui-fw/latest/js/tizen-web-ui-fw.js"
            data-framework-theme="tizen-white"></script>
     
     
        <script type="text/javascript" src="./js/main.js"></script>
        <link rel="stylesheet" type="text/css" href="./css/style.css"/>
        <head>
        <title></title>
        <script src="Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
        <link href="Styles/jqx.base.css" rel="stylesheet" type="text/css"/>
        <link href="Styles/jqx.classic.css" rel="stylesheet" type="text/css"/>
        <script src="Scripts/jqxcore.js" type="text/javascript"></script>
        <script src="Scripts/jqxbuttons.js" type="text/javascript"></script>
        <script src="Scripts/jqxdata.js" type="text/javascript"></script>
        <script src="Scripts/jqxgrid.js" type="text/javascript"></script>
        <script src="Scripts/jqxgrid.selection.js" type="text/javascript"></script>
        <script src="Scripts/jqxmenu.js" type="text/javascript"></script>
        <script src="Scripts/jqxscrollbar.js" type="text/javascript"></script>
     
     <script type="text/javascript">
           function soap() {
     
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.open('POST', 'http://www.webservicex.net/CurrencyConvertor.asmx?wsdl', true);
     
                // build SOAP request              
                     var sr =
               '<?xml version="1.0" encoding="utf-8"?>' +
               '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET/">'+
       		   '<soapenv:Header/>'+
       			'<soapenv:Body>'+
          		'<web:ConversionRate>'+
             '<web:FromCurrency>EUR</web:FromCurrency>'+
             '<web:ToCurrency>TND</web:ToCurrency>'+
          		'</web:ConversionRate>'+
       			'</soapenv:Body>'+
    			'</soapenv:Envelope>';
     
                xmlhttp.onreadystatechange = function () {
                	alert('onready');
                    if (xmlhttp.readyState == 4) {
                        if (xmlhttp.status == 200) {
     
                            alert('done use firebug to see response');
                        }
                    }
                    if (xmlhttp.readyState == 0) {
                        alert('open() has not been called yet');
                    }
     
                    if (xmlhttp.readyState == 1) {
                        alert('send() has not been called yet');
                    }
     
                    if (xmlhttp.readyState == 2) {
                        alert('send() has been called, ...');
                    }
     
                    if (xmlhttp.readyState == 3) {
                        alert('Interactive - Downloading');
                    }
                    else
                    alert('Consuming Error');
                }
                // Send the POST request
                xmlhttp.setRequestHeader("SOAPAction", "http://www.webservicex.net/CurrencyConvertor/");
                xmlhttp.setRequestHeader('Content-Type', 'text/xml');
                xmlhttp.send(sr);
                alert("Hi WS");
                // send request
                // ...
            }
     
        </script>
     
    </head>
     
    <body>
        <!-- Start of first page: #one -->
        <div data-role="page" id="one">
            <div data-role="header" data-position="fixed">
                <h1>Multi-page application</h1>
            </div><!-- /header -->
     
            <div data-role="content">
                <h2>One</h2>
                <p>
                    Main Page
                </p>
                <h3>Show internal pages:</h3>
               	<form>
               		<input type="button" value="WS" onclick="soap();">
               	</form>
                <p><a href="#two" data-role="button">Show page "two"</a></p>
                <p><a href="#popup"  data-role="button" data-position-to="window" data-rel="popup">Show page "popup" (as a dialog)</a></p>
     
                <div id="popup" data-role="popup" data-style="center_title_1btn">
                    <div class="ui-popup-title">
                        <h1>Popup title</h1>
                    </div>
                    <div class="ui-popup-text">Pop-up dialog box, a child window that blocks user interact to the parent windows</div>
                    <a href="#" data-inline="true" data-role="button" onclick="javascript:$('#popup').popup('close');">Close</a>
                </div>
            </div><!-- /content -->
     
            <div data-role="footer" data-position="fixed">
                <h4>Page Footer</h4>
            </div><!-- /footer -->
        </div><!-- /page one -->
     
     
        <!-- Start of second page: #two -->
        <div data-role="page" id="two">
            <div data-role="header" data-position="fixed">
                <h1>Two</h1>
            </div><!-- /header -->
     
            <div data-role="content">
                <h2>Two</h2>
                <p>Page 2</p>
                <p><a href="#one" data-direction="reverse" data-role="button">Back to page "one"</a></p>
            </div><!-- /content -->
     
            <div data-role="footer" data-position="fixed">
                <h4>Page Footer</h4>
            </div><!-- /footer -->
        </div><!-- /page two -->
     
    </body>
    </html>

  11. #11
    Expert éminent
    Avatar de sekaijin
    Homme Profil pro
    Urbaniste
    Inscrit en
    Juillet 2004
    Messages
    4 205
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Urbaniste
    Secteur : Santé

    Informations forums :
    Inscription : Juillet 2004
    Messages : 4 205
    Points : 9 127
    Points
    9 127
    Par défaut
    juste une question
    ton service web est dans le même domaine que ta page web ?
    si non tu as un PB car les navigateur interdit les appels xhr cross domain

    A+JYT

  12. #12
    Débutant  
    Inscrit en
    Mai 2006
    Messages
    705
    Détails du profil
    Informations forums :
    Inscription : Mai 2006
    Messages : 705
    Points : 117
    Points
    117
    Par défaut
    ton service web est dans le même domaine que ta page web ?
    En effet, je suis en train de développer une application mobile pour la consommation de ce web service

  13. #13
    Débutant  
    Inscrit en
    Mai 2006
    Messages
    705
    Détails du profil
    Informations forums :
    Inscription : Mai 2006
    Messages : 705
    Points : 117
    Points
    117
    Par défaut
    Je me demande si l'un parmi vous a testé le code ci-dessus?

Discussions similaires

  1. Réponses: 2
    Dernier message: 14/01/2013, 22h28
  2. consommé web service
    Par franfr57 dans le forum Windev Mobile
    Réponses: 0
    Dernier message: 20/09/2011, 11h49
  3. [PHP 5.0] Consommation web services
    Par tomtom7480 dans le forum Langage
    Réponses: 0
    Dernier message: 19/05/2011, 15h38
  4. Consommation Web Service en RPG
    Par parzifal dans le forum AS/400
    Réponses: 1
    Dernier message: 10/12/2010, 21h27
  5. Client .Net consommant web service en php
    Par Tsubasa dans le forum Général Dotnet
    Réponses: 1
    Dernier message: 05/05/2007, 09h58

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