';访问控制允许原点';Google finance converter提供了一个错误javascript/jquery
我正在使用一个项目模块,我想从Google finance converter获得基于货币代码和金额的转换价格。 这是我的密码:';访问控制允许原点';Google finance converter提供了一个错误javascript/jquery,javascript,jquery,asp.net-mvc,google-finance,Javascript,Jquery,Asp.net Mvc,Google Finance,我正在使用一个项目模块,我想从Google finance converter获得基于货币代码和金额的转换价格。 这是我的密码: $('.actual-price').tooltip({ content: function(callback) { var url = 'https://www.google.com/finance/converter?a=25&from=USD&to=AMD'; $.get(url, {}, function
$('.actual-price').tooltip({
content: function(callback) {
var url = 'https://www.google.com/finance/converter?a=25&from=USD&to=AMD';
$.get(url, {}, function(data) {
callback(data);
});
},
open: function(event, ui) {
var $id = $(ui.tooltip).attr('id');
$('div.ui-tooltip').not('#' + $id).remove();
},
close: function(event, ui) {
$('.ui - tooltip').hide();
}
});
这给了我一个错误:
无法加载XMLHttpRequest
https://www.google.com/finance/converter?a=25&from=USD&to=AED
。不
“Access Control Allow Origin”标头出现在请求的服务器上
资源。来源<代码>http://mytestsite.com因此不允许使用
进入
我尝试了以下方法来解决这个问题,但似乎没有什么对我有效
第一:
在web配置中添加了Access Control Allow Origin
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
并已转介下列职位:
Side:它正在使用我的本地主机,但当我尝试使用另一个域时,它会给我一个错误 当您发送请求时,必须同时在侧面允许允许访问来源。如果没有,您可以安装一些绕过它或下载整个页面(php)结束,然后处理它,但您也可以尝试:
为什么你的应用程序在本地主机上运行?我认为这是因为Google设置了access origin,允许从他们的域和本地主机进行连接 您需要更改Ajax设置
async:false
,如下所示:
JavaScript代码:
$('.actual-price').tooltip({
content: function(callback) {
var url = 'https://www.google.com/finance/converter?a=25&from=USD&to=AMD';
$.ajaxSetup({async: false}); ////////// Added
$.get(url, {}, function(data) {
callback(data);
});
},
open: function(event, ui) {
var $id = $(ui.tooltip).attr('id');
$('div.ui-tooltip').not('#' + $id).remove();
},
close: function(event, ui) {
$('.ui - tooltip').hide();
}
});
如果您在本地使用chrome,则在没有web服务器的情况下尝试从本地向另一台服务器发出请求时,这是一个错误
当您将应用程序上载到生产中的服务器时,应用程序将正常工作。因为这只是一个本地问题,浏览器会阻止该类型的请求。您正在调用此url,但此url不会返回它们在html表单中显示的任何内容,因此无法使用$调用。get@PareshGami:那它为什么和我的本地主机一起工作?如果你把它放在运行的实时服务器上,你的意思是?@PareshGami:它和我的本地主机一起工作localhost,但当我尝试使用另一个域时,它会给我一个错误!将此安装到浏览器时会发生什么情况?对我来说,它工作正常:。你的地址可能不正确。好的,让我再检查一次,会让你知道的!我正在为最终用户开发一个产品(插件),我不能要求最终用户安装特定的浏览器和扩展!正如我所说的,这将发生在您的本地开发中,当应用程序投入生产时,这不会发生,而这不是一个解决方案!!请参阅问题下方Samuel的评论。这对您来说不再是问题,实际上,这只是开发模式上的问题,您可以安装扩展或将应用程序放在web服务器上,这样您就可以提出跨域请求。在本地,这不是必需的,所以您可以在开发时安装一个插件来解决这个问题,但在生产模式下,当您部署应用程序时,这不是问题,因为不会发生。我和RubyonRails的情况一样,这就是解决方案。不要老是浪费时间。试着在生产模式下启动你的应用程序,看看错误是否存在,它不会出现。另一个选择是将jsonp与jquery一起使用,但正如我所说的,这不是必需的。祝你过得愉快,我的朋友
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);
});
$('.actual-price').tooltip({
content: function(callback) {
var url = 'https://www.google.com/finance/converter?a=25&from=USD&to=AMD';
$.ajaxSetup({async: false}); ////////// Added
$.get(url, {}, function(data) {
callback(data);
});
},
open: function(event, ui) {
var $id = $(ui.tooltip).attr('id');
$('div.ui-tooltip').not('#' + $id).remove();
},
close: function(event, ui) {
$('.ui - tooltip').hide();
}
});