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)

Cross Domain Data Access, GoogleApi, Javascripting and CORS

(0) ShareShare
ReportReport
Posted on by

Hello,

I have implemented Address AutoComplete functionality that works really well using this:

            jQuery.post('maps.googleapis.com/.../json{' + searchString + '}&types=address&language=en&crossDomain=true&key=[ourkey]', function (addresses) {
                
                try {
                    //Rewrapp address in CRM Results object
                    resultSet.results = new Array();
                    addressesFound = [];

                    //break;

                    if (addresses['predictions'].length > 0) {
                        for (var i = 0; i < addresses['predictions'].length; i++) {
                            addressesFound.push({
                                id: addresses['predictions'][i].place_id,
                                fields: [addresses['predictions'][i].description]
                            });
                        }
                        resultSet.results = addressesFound;
                    }

                    if (resultSet.results.length > 0) {
                        ext.getEventSource().showAutoComplete(resultSet);
                    } else {
                        ext.getEventSource().hideAutoComplete();
                    }
                }
                catch (e) {
                    alert("AutoComplete ErrorInt: " + e.message);
                }
            });


Problem we're have now is that IE browsers need to have "Access Data Sources Across Domains" ENABLED.
This does not fit into our corporate security policy.

I've tried using this:

            var xhr = XMLHttpRequest();
            xhr.onreadystatechange = HandleAddressesReturned;
            xhr.open('GET', 'maps.googleapis.com/.../json{' + searchString + '}&types=address&language=en&crossDomain=true&key=[Ourkey]');
            xhr.send(null);            


But I'm not seeing any data come back from Google. I'm sure I've made an error in implementation but cannot see what.

The callback function is this:

            function HandleAddressesReturned() {
                try {

                    if (xhr.readyState == 4) {
                        var addresses = xhr.responseText;

                    //DO SOME ADDRESS SELECTION HANDLING HERE

                    }
                }
                catch (e) {
                    alert("AutoComplete ErrorInt: " + e.message);
                }
            }


Can anyone point me in the right direction to negate the CORS issues restricting this functionality in IE?

Thanks!

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi,

    Try with this -

    var ajax = new XMLHttpRequest();
    var url = 'maps.googleapis.com/.../json{' + searchString + '}&types=address&language=en&crossDomain=true&key=[ourkey]';
    ajax.open("GET", url, true);
    ajax.onload = function () {
        var response = JSON.parse(ajax.responseText);
        if (!response || !response.results || !response.results[0]) {
            alert(response.error.message);
            return;
        }   
    };
    ajax.send();


  • gdas Profile Picture
    50,091 Moderator on at

    Hi,

    Does your CRM is using https or http ? 

    You can also test in google chrome browser whether the URL you are triggering that is working state or not.Just put a debugger in your code and using add watch get the complete URL and paste it in the google chrome browser . Check whether you are getting direct response or not.

    Let me know.

  • Community Member Profile Picture
    on at

    Thanks Goutam, great help.

    Currently getting and "invalid character" error after var response = JSON.parse(ajax.responseText);

    I will do what you say and check google.

    Address lookup works using a jQuery.Post call but CORS blocks that

  • Community Member Profile Picture
    on at

    Goutam, I grabbed responsetext from the watch. stripped out some unnecessary stuff.

    Note our url is prepended to the call to Google. That does not seem right to me.

    This call works, just NOT cross-domain:

    jQuery.post('maps.googleapis.com/.../json{' + searchString + '}&types=address&language=en&crossDomain=true&key=XXXXXXX', function (addresses) { // do stuff, handle addresses }

    --------------------------------------------------------------

    responseText "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"www.w3.org/.../xhtml1-strict.dtd\"> \n<html xmlns=\"http://www.w3.org/1999/xhtml\"> \n<head> \n<title>IIS 8.5 Detailed Error - 500.0 - Internal Server Error</title> ...

    <body> \n<div id=\"content\"> \n<div class=\"content-container\"> \n  <h3>HTTP Error 500.0 - Internal Server Error</h3> \n  <h4>Internal Server Error</h4> \n</div> \n<div class=\"content-container\"> \n <fieldset><h4>Most likely causes:</h4> \n  <ul> \t<li>IIS received the request; however, an internal error occurred during the processing of the request. The root cause of this error depends on which module handles the request and what was happening in the worker process when this error occurred.</li> \t<li>IIS was not able to access the web.config file for the Web site or application. This can occur if the NTFS permissions are set incorrectly.</li> \t<li>IIS was not able to process configuration for the Web site or application.</li> \t<li>The authenticated user does not have permission to use this DLL.</li> \t<li>The request is mapped to a managed handler but the .NET Extensibility Feature is not installed.</li> </ul> \n </fieldset> \n</div> \n<div class=\"content-container\"> \n <fieldset><h4>Things you can try:</h4> \n  <ul> \t<li>Ensure that the NTFS permissions for the web.config file are correct and allow access to the Web server's machine account.</li> \t<li>Check the event logs to see if any additional information was logged.</li> \t<li>Verify the permissions for the DLL.</li> \t<li>Install the .NET Extensibility feature if the request is mapped to a managed handler.</li> \t<li>Create a tracing rule to track failed requests for this HTTP status code. For more information about creating a tracing rule for failed requests, click <a href=\"go.microsoft.com/fwlink\">here</a>. </li> </ul> \n </fieldset> \n</div> \n \n<div class=\"content-container\"> \n <fieldset>

    <h4>Detailed Error Information:</h4> ....

    <th>Requested URL</th>

    http:// [mytesturl] /maps.googleapis.com/maps/api/place/queryautocomplete/json?input=&#x7b;55stabe&#x7d;&types=address&language=en&crossDomain=true&key=XXXXXXXX

    <h4>More Information:</h4> \n  This error means that there was a problem while processing the request. The request was received by the Web server, but during processing a fatal error occurred, causing the 500 error.

    ---------------------------------------------------------------------------------------

    No idea what's wrong right now

  • Community Member Profile Picture
    on at

    Oh, and this forum modifies my urls that I input here so you're not seeing the right thing

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi,

    Seems the error is coming from google API, may be the request which you are sending not the correct request. The url should response valid JSON when you are posting directly in chrome browser.

    "IIS received the request; however, an internal error occurred during the processing of the request. "

    Not sure about the URL, you may try contact google API support help desk so that they can give you sample URL request format .

  • Community Member Profile Picture
    on at

    I hear you. As I said, the urls posted here get modified on posting so you're not seeing what I actually have.

    The url I use works just fine on a jQuery.Post call.

    I'll try once more, doubt it'll work:

    maps.googleapis.com/.../json{' + searchString + '}&types=address&language=en&crossDomain=true&key=XXXXXXXKey'

    if I don't include https the url being hit is prepended with my crm url.

    So still open to suggestions. I'll continue tomorrow.

    Thanks for your help.

  • Community Member Profile Picture
    on at

    if you hover over that link, you'll see full query bottom of browser... THAT works hitting google. Just not using this XMLHttpRequest

  • Suggested answer
    Emre GULCAN Profile Picture
    2,379 on at

    Hi,

    We discussed before same subject at community.dynamics.com/.../272707 , maybe it can helps you. If you're using D365 online CORS http post is not working so solution is using "Custom Action" and call it from your form/webresource.

    Inside your custom action call Google or other CORS webservices

  • Community Member Profile Picture
    on at

    QUESTION: (for anyone who knows...)

    If I implement whatever changes in IIS and CRM to enable this CORS query, does the "cross domain data access" still have to be enabled in the browser, regardless?

    Or does enabling CORS in IIS and CRM bypass the browser setting?

    Seems like an odd question but our security team does not want to adjust group policy on the browser (IE 11) to allow cross domain data access and if that blocks it all, there's no point in doing the work in IIS and CRM.

    Thanks

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