Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 货币兑换脚本不工作_Javascript_Jquery_Json_Html - Fatal编程技术网

Javascript 货币兑换脚本不工作

Javascript 货币兑换脚本不工作,javascript,jquery,json,html,Javascript,Jquery,Json,Html,由于某些原因,我无法获得JSON的值,有人能帮我吗 function forex() { var to = document.getElementById("to").value; alert(to); var from = document.getElementById("from").value; alert(from); var amount = document.getElementById("amount").value; alert(a

由于某些原因,我无法获得JSON的值,有人能帮我吗

function forex() {
    var to = document.getElementById("to").value;
    alert(to);
    var from = document.getElementById("from").value;
    alert(from);
    var amount = document.getElementById("amount").value;
    alert(amount);
    $.getJSON("http://rate-exchange.appspot.com/currency?from=" + from + "&to=" + to + "&q=" + amount, function (response) {
        alert(response);
    });

    }

由于,您无法通过ajax访问rate-exchange.appspot.com上的资源,因为您的Javascript在不同的域上执行

在您的情况下,此特定站点支持,因此您可以使用jQuery的JSONP实现来绕过同源策略。JSONP通过将目标URL包含为
标记并使用回调来工作,回调不受同源策略的约束

$.getJSON()
方法如果在URL中找到一个参数,例如
callback=?
,将使用JSONP

例如:

function forex() {
    var to = document.getElementById("to").value;
    var from = document.getElementById("from").value;
    var amount = document.getElementById("amount").value;

    $.getJSON("http://rate-exchange.appspot.com/currency?&callback=?", { from : from, to : to, q : amount }, function (response) {
        alert(response.v);
    });
}
我还将手动URL变量更改为首选对象,因为jQuery将处理URL编码。

由于,您无法通过ajax访问rate-exchange.appspot.com上的资源,因为您的Javascript在不同的域上执行

在您的情况下,此特定站点支持,因此您可以使用jQuery的JSONP实现来绕过同源策略。JSONP通过将目标URL包含为
标记并使用回调来工作,回调不受同源策略的约束

$.getJSON()
方法如果在URL中找到一个参数,例如
callback=?
,将使用JSONP

例如:

function forex() {
    var to = document.getElementById("to").value;
    var from = document.getElementById("from").value;
    var amount = document.getElementById("amount").value;

    $.getJSON("http://rate-exchange.appspot.com/currency?&callback=?", { from : from, to : to, q : amount }, function (response) {
        alert(response.v);
    });
}

我还将手动URL变量更改为首选对象,因为jQuery将处理URL编码。

而不是下面的代码:

$.getJSON("http://rate-exchange.appspot.com/currency?from=" + from + "&to=" + to + "&q=" + amount, function (response) {
   alert(response);
});
使用以下代码:

$.ajax({
  url: "http://rate-exchange.appspot.com/currency?from=" + from + "&to=" + to + "&q=" + amount,
  dataType: 'jsonp',
  success: function(response) {
        alert(response.v);
  }
});

而不是以下代码:

$.getJSON("http://rate-exchange.appspot.com/currency?from=" + from + "&to=" + to + "&q=" + amount, function (response) {
   alert(response);
});
使用以下代码:

$.ajax({
  url: "http://rate-exchange.appspot.com/currency?from=" + from + "&to=" + to + "&q=" + amount,
  dataType: 'jsonp',
  success: function(response) {
        alert(response.v);
  }
});

检查控制台是否有错误,然后尝试
console.log(response)
是否在运行此脚本?检查控制台是否有错误,然后尝试
console.log(response)
是否在运行此脚本?@roee69可能是您需要更改URL。@roee69可能是您需要更改URL。