Hin*_*man 6 javascript asp.net-mvc jquery google-finance
我正在使用一个项目模块,我想在该模块中根据货币代码和金额从 Google 财务转换器获取换算后的价格。\n这是我的代码:
\n\n$(\'.actual-price\').tooltip({\n content: function(callback) {\n var url = \'https://www.google.com/finance/converter?a=25&from=USD&to=AMD\';\n $.get(url, {}, function(data) { \n callback(data);\n });\n },\n open: function(event, ui) {\n var $id = $(ui.tooltip).attr(\'id\');\n $(\'div.ui-tooltip\').not(\'#\' + $id).remove();\n },\n close: function(event, ui) {\n $(\'.ui - tooltip\').hide();\n }\n});\nRun Code Online (Sandbox Code Playgroud)\n\n它给了我一个错误:
\n\n\n\n\nXMLHttpRequest 无法加载\n
\nhttps://www.google.com/finance/converter?a=25&from=USD&to=AED。所请求的资源上不存在“Access-Control-Allow-Origin”标头。http://mytestsite.com因此不允许源访问。
我尝试过以下方法来解决它,但似乎没有什么对我有用!
\n\n首先:\n添加Access-Control-Allow-Origin到网络配置中。
<httpProtocol>\n <customHeaders>\n <add name="Access-Control-Allow-Origin" value="*" />\n </customHeaders>\n</httpProtocol>\nRun Code Online (Sandbox Code Playgroud)\n\n第二:使用数据类型 jsonp:
\n\ndataType: "jsonp",\nRun Code Online (Sandbox Code Playgroud)\n\n并已经提到了以下帖子:
\n\n\xe2\x80\x9c请求的资源上不存在“Access-Control-Allow-Origin\”标头\xe2\x80\x9d
\n\n请求的资源错误上不存在“Access-Control-Allow-Origin”标头
\n\n请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问 Origin \'\xe2\x80\xa6\'
\n\n\xe2\x80\x9c请求的资源上不存在“Access-Control-Allow-Origin\”标头\xe2\x80\x9d
\n\n侧面:它正在与我的本地主机一起使用,但是当我尝试使用另一个域时,它给了我一个错误!
\n当您发送请求时,允许访问源也必须被允许。如果没有,您可以安装一些浏览器扩展来绕过它或下载整个页面(php)结束然后处理它,但您也可以尝试cors-anywhere.herokuapp.com:
var myUrl = 'http://www.geoplugin.net/json.gp?ip=216.58.209.68';
var proxy = 'https://cors-anywhere.herokuapp.com/';
var finalURL = proxy + myUrl;
// With the get JSON (frequently used) method
$.getJSON(finalURL, function( data ) {
console.log(data);
});
// With the get method
$.get(finalURL, function( data ) {
console.log(data);
});
// With the post method
$.post(finalURL, function( data ) {
console.log(data);
});
Run Code Online (Sandbox Code Playgroud)
更多这里。
为什么你的应用程序在本地主机上运行?我认为这是因为 Google 已将访问源设置为允许从其域和本地主机进行连接。
| 归档时间: |
|
| 查看次数: |
901 次 |
| 最近记录: |