问题描述
我尝试通过Ajax从.js文件中调用在Google App脚本上发布的脚本。 当我直接在浏览器上使用url时,它工作得很好(我得到了Json的回答),但是当调用时,我不断收到“ MIME类型为text / html的跨域读取阻止(CORB)阻止跨域响应”是从Ajax本地完成的。
任何想法如何解决??
function doGet(e) {
Logger.log('I was called finaly');
var data = "some data";
var result = JSON.stringify({data: data});
return ContentService
.createtextoutput(e.parameter.callback + "(" + result + ")")
.setMimeType(ContentService.MimeType.JAVASCRIPT);
}
let callGoogleScript = (user) => {
console.log("callGoogleScript triggered");
var link = "https://script.google.com/macros/s/123/exec";
var url = link + "?callback=answer&name=";
$.ajax({
crossDomain: true,url: url + encodeURIComponent(user),method: "GET",dataType: 'jsonp'
});
}
解决方法
无需定义dataType JsonP
对此有类似的answer。您的AJAX请求正在尝试使用填充dataType: 'jsonp'
检索JSON。
按照Jquery Documentation > JSONP说If the URL includes the string "callback=?" (or similar,as defined by the server-side API),the request is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details.
总结一下,将您的 dataType 更改为json
参考
,要使用JSONP
,应将网址放在脚本标签中:
<script src = "https://script.google.com/macros/s/123/exec?callback=answer&name="></script>
然后将调用您的Web应用程序中的 callback
函数(即answer
)。可以使用getScript()
请参见Serving JSONP in your web pages。如果您在生产环境中使用JSONP
,请确保您了解使用Scenario: Conversion completes immediately
Given ...
When I start the conversion
Then I should see the conversion is completed
Scenario: Conversion will take a long time to complete
Given ...
When I start the conversion
Then I should see the conversion is progressing
的所有安全隐患。