Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

cross-domain Ajax POST inside webresource

Posted on by 2,379

Hi all,

I've a requirement that to call external service with AJAX POST method. This service supports CORS and working correctly from any webpage, however I get exception (Access Denied) when I try to post data inside Dynamics 365 online webresource file.

Just simple code is below;

var request = {
    "Data": "My parameter",
    "Validation": "My validation"
};


$.ajax({
    url: _myExternalServiceUrl,
    async: true,
    type: 'POST',
    data: JSON.stringify(request),
    contentType: "application/json; charset=utf-8",
    crossDomain: true,
    dataType: 'json',
    beforeSend: function (xhr) {
        $("#loadingbar").show();
    },
    complete: function () {
        $("#loadingbar").hide();
    },
    success: function (response) {
        console.log(JSON.stringify(response));
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert("exception : " + xhr.statusText);
    }
});

I think maybe it's about "POST" to cross domain, because JSONP and GET method allows crossdomain calls.

Do you have any idea, suggestion for this?

Kind regards

*This post is locked for comments

  • Emre GULCAN Profile Picture
    Emre GULCAN 2,379 on at
    RE: cross-domain Ajax POST inside webresource

    Hi Mohammad,

    Thank you, your answer is same with Andrew's solution.

    Probably I'll try this way and use external service inside Action.

  • Verified answer
    ARIFNIIT Profile Picture
    ARIFNIIT 1,391 on at
    RE: cross-domain Ajax POST inside webresource

    Alternate option : you can create Action to call external Web API and you can call this action using JS.

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: cross-domain Ajax POST inside webresource

    Hello,

    When you have to return complex object I would suggest to use plain string, serialize your object of any complexity to JSon and deserialize it on webresource side.

  • Emre GULCAN Profile Picture
    Emre GULCAN 2,379 on at
    RE: cross-domain Ajax POST inside webresource

    Hi Andrew,

    Thank you for your response. It looks like a tricky but probably problem solver. I discussed with my client to convert their webservice to use GET method with querystring parameters instead of POST.

    I took a quick look to your recommendation and it returns just 2 output parameters, however my external service returns complex object dataset, how can I return this from Action? I think maybe "Text" output solves my requirements and after that I parsed it inside my webresource, do you have any experience about that?

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: cross-domain Ajax POST inside webresource

    Hello,

    Check this - salimadamoncrm.com/.../crm-2013-actions-to-get-around-javascript-cross-domain-challenges

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans