web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Ajax jquery call is not working

(0) ShareShare
ReportReport
Posted on by 902

Hi,

below code is perfectly working as html form but when I am using in CRM as web resource or I frame that time I have got error. Error text is empty.

Please open this code as .html and you can see it is working fine.

Question 

1. Ajax jquery call is supported in latest version of dynamic 365 ?

2. Please let me know if any thing wrong in this code ?

Please do needful.

Code

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link rel="stylesheet" href="cdn.datatables.net/.../jquery.dataTables.min.css" />
    <link rel="stylesheet" href="maxcdn.bootstrapcdn.com/.../bootstrap.min.css" />
    <link rel="stylesheet" href="cdnjs.cloudflare.com/.../font-awesome.min.css">
</head>
<body>
    <button class="btn btn-info" onclick="ShowData()">Show</button>
    <hr />
    <table id="data" class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>ID</th>
                <th>City</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody></tbody>
        <tfoot>
            <tr>
                <th>ID</th>
                <th>City</th>
                <th>Status</th>
            </tr>
        </tfoot>
    </table>
    <script src="code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="maxcdn.bootstrapcdn.com/.../bootstrap.min.js"></script>
    <script src="cdn.datatables.net/.../jquery.dataTables.min.js"></script>
    <script>
        $(document).ready(function () {
            //GetData();
        })
        function ShowData() {
            GetData();
        }
        function GetData() {
        
            var url = "tech.centralindia.cloudapp.azure.com/.../City";
            //debugger;
            $.ajax({
                type: "GET",
                url: url,
                dataType: "json",
                contentType: 'application/json; charset=utf-8',
                beforeSend: function (XMLHttpRequest) {
                    XMLHttpRequest.setRequestHeader("Accept", "application/json");
                },
                success: function (data) {
                    debugger;
                    console.log(data);
                    BindData(data);
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    debugger;
                    alert(xhr.status);
                    alert(thrownError);
                }
            });
        }
        function BindData(data) {
            $("#data").dataTable().fnDestroy();
            $('#data').DataTable(
                {
                    "aaData": data.Data
                    , "aoColumns": [
                        { "sTitle": "ID", "mData": "CityId", className: "dt-head-center" },
                        { "sTitle": "City", "mData": "CityName", className: "dt-head-center" },
                        {
                            "sTitle": "Status", "mData": "IsActive", className: "dt-head-center",
                            "render": function (mData, type, full, meta) {
                                if (mData == true) {
                                    return '<i class="fa fa-check"></i>';
                                }
                                else {
                                    return '<i class="fa fa-remove"></i>';
                                }
                            }
                        }
                    ]
                });
        }
        
        
        
    </script>
</body>
</html>

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    jestuder Profile Picture
    158 on at

    We experienced this same issue with JavaScript that we have written.  What we noticed is if you turn on legacy forms then it works.  If you turn off legacy mode then it won't work.  What we ended up doing was moving all our ajax called to use SDK.REST library from the SDK.

  • Verified answer
    Community Member Profile Picture
    on at

    Hi Keyur,

    calling external webservice from javascript is a pain, cause there is a lot of variables link http vs https (cors), trusted origin, etc.

    The best approach is to use an action, please refer to these pages:

    http://arpitmscrmhunt.blogspot.it/2015/03/call-external-web-service-using-custom.html 

    http://blogs.microsoft.co.il/rdt/2016/01/13/executing-custom-action-via-javascript/ 

    Hope it helps!

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Keyur,

    if you solved the problem thanks to an answer, please mark it as verified to encourage the community to provide more and more a better support. Thank you. Francesco

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • keyur7379 Profile Picture
    902 on at

    Hey Francesco,

    Sorry for late response.

    Actually I have tried this solutions but didn't get success.

    I want to call webapi using ajax jquery call. Is it possible ?

    also want to know anything wrong in above code?

    I have made this api using MVC and then use of ajax jquery I am calling webapi and it is working fine as html in browser but when I register this html file in CRM as webresource then It won't work. I got access denied error.

    You can use above html code and make it as html file and open in browser directly. I am sure it is working well.

    Thanks,

    keyur shah.

  • Suggested answer
    imayur Profile Picture
    630 on at

    Use jQuery get method instead, Its much easier

    refer below snippet

    var url = 'http://someurl.com/apiendpoint';
    
    var result= $.get( url, function(data) {
    
    Console.log(data);
    
    return data;
    
     alert( "success" );
    
    }).fail(function() {javascript:void(0);
    
       alert( "error" );
    
     });


  • Community Member Profile Picture
    on at

    Hi keyur,

    try the following:

    $.ajax({
        url: YOUR_URL,
        type: "POST",
        dataType: "xml",
        contentType: "text/xml; charset=\"utf-8\"",
        data: YOUR_DATA,
        success: function(result){
           alert(result)
        },
        error: function (err) {
            alert(err)
        }
    });

    Please let me know if you solve

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi keyur,

    if you solved the problem thanks to an answer, please mark it as verified to encourage the community to provide more and more a better support. Thank you. Francesco

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Suggested answer
    keyur7379 Profile Picture
    902 on at

    Hi ,

    I got a solution.

    There is some cross platform issue in webapi. Also I have found one more issue is you have to enable SSL certificate for hosted URL.

    Thanks,

    keyur shah

  • Ashish Kapoor Profile Picture
    on at

    Hi Keyur,

    Can you please provide some more details around the resolution. I have the same issue where in the call from outside CRM as an html page works; however I get a 404 error in crm and the response always goes to the error function. I can see the actual JSON response as a callback in the network logs.

    Many thanks,

    Ashish

  • Suggested answer
    keyur7379 Profile Picture
    902 on at

    Ashish Kapoor 

    I am sure, it is issue of your api. First I have made api in visual studio 2013 but it didn't work for me(I don't know what is missing in the code or component) then I have made api in visual studio 2017 and it works for me.

    do one thing take test rest api from here https://jsonplaceholder.typicode.com/

    and check it. sure it is work in html web resource.

    Thanks,

    keyur shah.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans