如何在Dynamics CRM中使用CORS调用Google API

如何在Dynamics CRM中使用CORS调用Google API,cors,cross-domain,dynamics-crm,Cors,Cross Domain,Dynamics Crm,我从Dynamics CRM 365中的javascript Web资源中简单调用了google API var xhr = new XMLHttpRequest(); xhr.open("GET", 'https://maps.googleapis.com/maps/api/place/queryautocomplete/json?input={' + searchString + '}&types=address&language=en

我从Dynamics CRM 365中的javascript Web资源中简单调用了google API

            var xhr = new XMLHttpRequest();
        xhr.open("GET", 'https://maps.googleapis.com/maps/api/place/queryautocomplete/json?input={' + searchString + '}&types=address&language=en&crossDomain=true&key=[ourKey]', true);
        xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
        xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET,POST');
        xhr.onload = function () {
            var response = JSON.parse(xhr.responseText);

//do something with the response here

        };
        xhr.send();
我在IE中的开发工具中收到以下消息和错误:

*SEC7118:XMLHttpRequest for{55 lllll}&types=address&language=en&crossDomain=true&key=[ourKey]所需的跨源资源共享COR。 文件:ClientApiWrapper.aspx

SEC7119:XMLHttpRequest for{55 lllll}&types=address&language=en&crossDomain=true&key=[ourKey]需要CORS预飞行。 文件:ClientApiWrapper.aspx

SEC7120:在访问控制允许原点标头中找不到原点。 文件:ClientApiWrapper.aspx

SCRIPT7002:XMLHttpRequest:网络错误0x80070005,访问被拒绝。 文件:ClientApiWrapper.aspx*

翻阅CORS相关资料和指南,我相信我做的一切都是对的

我也尝试过这一点,尽管我预计这里会出现CORS问题: jQuery.post'{'+searchString+'}&types=address&language='+langCode+'&key=[ourKey]',函数地址{ 试一试{

//Do something with returned addresses here...

            }
            catch (e) {
                alert("AutoComplete ErrorInt: " + e.message);
            }
        });
我确信这是一个与Dynamics CRM相关的问题,Dynamics在自己的沙箱之外不能很好地发挥作用

有谁能在Dynamics CRM领域内建议一个修复方案吗

感谢

Access Control Allow Origin和Access Control Allow Methods标头是响应标头,它们是根据您的请求从服务器发送的

只需将其从xhr请求代码中完全删除即可。如果未明确指定,浏览器将为您添加源标题,而maps.googleapis.com上的服务器应添加所需的CORS响应标题Access Control Allow Origin和其他可能的标题

如果在那之后仍然遇到问题,请为GET请求和飞行前选项请求(如果有)发布一整套请求和响应头

编辑了以下评论

好的,下面是我从你的评论中看到的:

var xhr = new XMLHttpRequest();
xhr.open("GET", 'maps.googleapis.com/maps/api/place/queryautocomplete/…{' + 
searchString + '}&types=address&language=en&crossDomain=true&key=[ourKey]', 
true);
xhr.onload = function () {
    var response = JSON.parse(xhr.responseText);
    //do something with the response
};
xhr.send();

Errors: SEC7120: Origin http://[ourUrl] not found in Access-Control-Allow-Origin header. File: ClientApiWrapper.aspx

GET /maps/api/place/queryautocomplete/json?input={55%20saddl}&types=address&language=en&crossDomain=true&key=[ourKey] HTTP/1.1
Accept: / 
Referer: http://[ourURL]/[ourOrg]/form/ClientApiWrapper.aspx?ver=1166920881
Accept-Language: en-CA
Origin: http://[ourURL]
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: maps.googleapis.com
Connection: Keep-Alive
Cache-Control: no-cache

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Date: Wed, 25 Apr 2018 16:26:36 GMT
Expires: Wed, 25 Apr 2018 16:31:36 GMT
Cache-Control: public, max-age=300
Server: scaffolding on HTTPServer2
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
好的,那么浏览器正在发送源标题。这意味着您的服务器需要返回一些CORS响应标题

因此,您需要在服务器上添加代码,以返回选项、GET和POST请求的以下CORS响应头:

Access-Control-Allow-Origin: <value of Origin request header>
最后一个指定浏览器缓存飞行前选项响应的秒数,因此不需要每次都进行缓存。这是可选的,但建议为。86400秒=1天


这应该会让你走起来。

所以,在经历了这么多的混乱之后,我在Google API开发支持上发布了一个问题。下面是我得到的答案……希望它能帮助其他人尝试类似的东西:

谢谢你的发帖。 我可以看到您在HTTP请求中使用了Places API Web服务。Places API Web服务不打算在客户端应用程序上使用,因为响应不包含Access Control Allow Origin标头。请参阅重复问题中的详细信息。
要在JS应用程序中使用Places API,我建议您使用Maps Javascript API中的Places库。该库提供了一个可以在Places API web服务中找到的自动完成功能。如果您想要web服务的queryautocomplete功能,Javascript API提供了文档中所述的getQueryPredictions函数。

您需要配置Dynamics CRM以随请求一起发送其来源:http://[ourURL]标头。如果不可配置,您可以尝试自己设置:xhr.setRequestHeader'Origin',http://[ourURL]“;。是的,试过了。完整的URL和通配符……仍然是nada。我确信CRM会抑制标题的输出,即使它们已设置。有趣的是,在MS支持下提出了一个问题,他们说这是谷歌的问题。但是我在CRM之外工作,所以,没有。有其他建议吗?有人吗?在添加完整URL的标题后,我得到了这个:SEC7120:Origin在Access Control Allow Origin header中找不到http://[ourUrl]。文件:ClientApiWrapper.aspx以下是脚本、错误和来自开发工具F12的请求/响应头javascript:var xhr=new-XMLHttpRequest;xhr.openGET,“{'+searchString+'}&types=address&language=en&crossDomain=true&key=[ourKey]“,true;xhr.onload=function{var response=JSON.parsexhr.responseText;//对响应执行某些操作};xhr.send;下面是错误…错误:SEC7120:Origin http://[ourUrl]在Access Control Allow Origin标头中找不到。文件:ClientApiWrapper.aspx请求标头:请求:GET/maps/api/place/queryautocomplete/json?input={55%20saddl}&types=address&language=en&crossDomain=true&key=[ourKey]HTTP/1.1 Accept:/Referer:HTTP://[ourURL]/[ourOrg]/form/ClientApiWrapper.aspx?ver=1166920881接受语言:en-CA来源:http://[ourURL]接受编码:gzip,deflate用户代理:Mozilla/5.0 Windows NT 6.3;WOW64;Trident/7.0;rv:11.0 like Gecko主机:maps.googleapis.com连接:Keep-Alive缓存控制:无缓存响应头:响应:HTTP/1.1 200 OK内容类型:application/json;ch
arset=UTF-8日期:2018年4月25日星期三16:26:36 GMT到期时间:2018年4月25日星期三16:31:36 GMT缓存控制:公共,最大年龄=300服务器:HTTPServer2 X-XSS-Protection上的脚手架:1;mode=block X-Frame-Options:sameoriginory,我无法控制谷歌服务器。我在这里遵循了google上的CORS建议页面:以及stack和其他地方的一些帖子,因此我的查询包括crossDomain=true参数。所以我猜我调用的GoogleAPI与CORS不兼容。。。?或者我可以在脚本中做些什么,比如设置飞行前选项来启用它,或者让google服务器发送回适当的标题?
Access-Control-Allow-Methods: <value-of-Access-Control-Request-Method request header>
Access-Control-Allow-Headers: <value-of-Access-Control-Request-Headers request header>
Access-Control-Max-Age: 86400