Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Call web service from new Web API Javascript

Posted on by 510

Hi guys.

Is there any different new way to call an external web service from a web resource javascript file using the new web api?

Regards.

*This post is locked for comments

  • Emre GULCAN Profile Picture
    Emre GULCAN 2,379 on at
    RE: Call web service from new Web API Javascript

    Hi,

    Your first/main problem is you don't get exception details on your request "onerror", maybe your url is not reachable or your payload is not correct.

    Please look at this pages

    https://stackoverflow.com/questions/124269/simplest-soap-example 

    https://thycotic.force.com/support/s/article/Using-Web-Services-with-SOAP-Javascript

    and important point is here;

    if (this.status === 204) {
       //success
    } else {
       //exception
       var exceptionDetails = this.statusText;
    }


    "this.statusText" gives you more details

  • HenriquePalomo Profile Picture
    HenriquePalomo 510 on at
    RE: Call web service from new Web API Javascript

    var url = "xxxxxxxx.azurewebsites.net/.../xxxxxxx.svc;;

           var xhr = new XMLHttpRequest();

           xhr.open("POST", url, true);

           //var xhr = createCORSRequest('POST', url);

           if (!xhr) {

               throw new Error('CORS not supported');

           }

           else {

               var text =

                   "<s:Envelope xmlns:s='schemas.xmlsoap.org/.../&;>" +

                   "<s:Header>" +

                       "<Action s:mustUnderstand='1' xmlns='schemas.microsoft.com/.../none&;> tempuri.org/.../SearchAddress </Action>" +

                   "</s:Header>" +

                   "<s:Body>" +

                       "<SearchAddress xmlns='http://tempuri.org/&#39;>" +

                   "<searchAddress>asd</searchAddress>" +

                       "</SearchAddress>" +

                   "</s:Body>" +

                   "</s:Envelope>";

               //parser = new DOMParser();

               //var xmlDoc = parser.parseFromString(text, "text/xml");

               xhr.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

               xhr.setRequestHeader('SOAPAction', 'tempuri.org/.../SearchAddress&;);

               xhr.onload = function (response_) {

                   var responseText = xhr.responseText;

                   console.log(responseText);

                   alert('Got Results');

               };

               xhr.onerror = function () {

                   console.log('There was an error!');

                   alert('Bad request');

               };

               xhr.send(text);

  • Emre GULCAN Profile Picture
    Emre GULCAN 2,379 on at
    RE: Call web service from new Web API Javascript

    Hi,

    Please share your code. If you get "bad request" it's Http 400 and probably you try to wrong method to call your external webservice (for exp: GET or POST) or your request object (payload) is not correct or not serialized correctly.

  • HenriquePalomo Profile Picture
    HenriquePalomo 510 on at
    RE: Call web service from new Web API Javascript

    Hi Emre. Thanks for the repsonse.

    Im getting bad request, but actually i tried to call the web service from SOAPUI and it worked like a charm, the problem is to call the WS inside the JavaScript

  • Suggested answer
    Emre GULCAN Profile Picture
    Emre GULCAN 2,379 on at
    RE: Call web service from new Web API Javascript

    Hi Henrique,

    D365 v9 has some changes and deprecated methods on Xrm client API, but not related how can you use / call external webservices.

    If you get any exception when using XmlHttp please inform me about your exception maybe I can help you.

  • HenriquePalomo Profile Picture
    HenriquePalomo 510 on at
    RE: Call web service from new Web API Javascript

    Ok, thanks Emre.

    Actually I'm using XmlHttp, but I thought that something could be broken or deprecated because of the update to V9, but I didn't found any articles about that as well.

    So thanks for the answer.

    Regards.

  • Verified answer
    Emre GULCAN Profile Picture
    Emre GULCAN 2,379 on at
    RE: Call web service from new Web API Javascript

    Hi,

    You can call external web servisce using jQuery Ajax (api.jquery.com/jquery.ajax) or directly XmlHttp request (www.w3schools.com/.../xml_http.asp or developer.mozilla.org/.../XMLHttpRequest), it's not related Dynamics CRM Web API

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans