货币转换器与JSONp

时间:2013-12-12 19:48:45

标签: javascript jquery html css jsonp

我正在努力构建JSONP和我的货币转换器。我认为这是一件非常简单的事情,因为我在编程等方面还很新。

我有HTML,其中用户可以输入他们想要转换的号码,并选择从和两种货币。当他们单击转换时,会显示一条加载消息,但不会发生任何其他情况。非常感谢任何和所有的帮助!

JQuery的:

$(function() {
$('#btnConvert').click(function() {

$('#result').html('Loading...');

var amount = $('#amount').val();
var from = $('#from').val();
var to = $('#to').val();

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

以下是我的HTML的正文部分:

 <form id="form1" runat="server">
 <table>
 <tr><td align="right">Enter Amount:</td><td>  <input id="amount" maxlength="12" size="5" value="1" /></td></tr>
 <tr><td align="right">From:</td><td>
<select id="from">
<option value="AED">United Arab Emirates Dirham (AED)</option>
<option value="ANG">Netherlands Antillean Guilder (ANG)</option>
<option value="BWP">Botswanan Pula (BWP)</option>
<option value="HKD">Hong Kong Dollar (HKD)</option>
<option value="HNL">Honduran Lempira (HNL)</option>
<option value="HRK">Croatian Kuna (HRK)</option>
<option value="HUF">Hungarian Forint (HUF)</option>
<option value="IDR">Indonesian Rupiah (IDR)</option>
<option value="ILS">Israeli New Sheqel (ILS)</option>
<option value="MAD">Moroccan Dirham (MAD)</option>
<option value="MDL">Moldovan Leu (MDL)</option>
<option value="MKD">Macedonian Denar (MKD)</option>
<option value="MUR">Mauritian Rupee (MUR)</option>
<option value="MVR">Maldivian Rufiyaa (MVR)</option>
<option value="MXN">Mexican Peso (MXN)</option>
<option value="MYR">Malaysian Ringgit (MYR)</option>
<option value="NAD">Namibian Dollar (NAD)</option>
<option value="TTD">Trinidad and Tobago Dollar (TTD)</option>
<option value="TWD">New Taiwan Dollar (TWD)</option>
<option value="TZS">Tanzanian Shilling (TZS)</option>
<option value="UAH">Ukrainian Hryvnia (UAH)</option>
<option value="UGX">Ugandan Shilling (UGX)</option>
<option value="USD" selected>US Dollar (USD)</option>

</select></td>
</tr>
<tr> <td align="right">to:</td><td>
<select id="to">
<option value="AED">United Arab Emirates Dirham (AED)</option>
<option value="ANG">Netherlands Antillean Guilder (ANG)</option>
<option value="ARS">Argentine Peso (ARS)</option>
<option value="AUD">Australian Dollar (AUD)</option>
<option value="EGP">Egyptian Pound (EGP)</option>
<option value="EUR">Euro (EUR)</option>
<option value="FJD">Fijian Dollar (FJD)</option>
<option value="GBP">British Pound Sterling (GBP)</option>
<option value="HKD">Hong Kong Dollar (HKD)</option>
<option value="HNL">Honduran Lempira (HNL)</option>
<option value="HRK">Croatian Kuna (HRK)</option>
<option value="HUF">Hungarian Forint (HUF)</option>
<option value="IDR">Indonesian Rupiah (IDR)</option>
<option value="ILS">Israeli New Sheqel (ILS)</option>
<option value="INR" selected>Indian Rupee (INR)</option>
<option value="JMD">Jamaican Dollar (JMD)</option>
<option value="JOD">Jordanian Dinar (JOD)</option>
<option value="JPY">Japanese Yen (JPY)</option>
<option value="MVR">Maldivian Rufiyaa (MVR)</option>
<option value="MXN">Mexican Peso (MXN)</option>
<option value="MYR">Malaysian Ringgit (MYR)</option>
<option value="NAD">Namibian Dollar (NAD)</option>
<option value="NGN">Nigerian Naira (NGN)</option>
<option value="NIO">Nicaraguan Córdoba (NIO)</option>
<option value="NOK">Norwegian Krone (NOK)</option>
<option value="NPR">Nepalese Rupee (NPR)</option>
<option value="NZD">New Zealand Dollar (NZD)</option>
<option value="OMR">Omani Rial (OMR)</option>
<option value="PEN">Peruvian Nuevo Sol (PEN)</option>
<option value="PGK">Papua New Guinean Kina (PGK)</option>
<option value="PHP">Philippine Peso (PHP)</option>
<option value="PKR">Pakistani Rupee (PKR)</option>
<option value="PLN">Polish Zloty (PLN)</option>
<option value="PYG">Paraguayan Guarani (PYG)</option>
<option value="QAR">Qatari Rial (QAR)</option>
<option value="RON">Romanian Leu (RON)</option>
<option value="RSD">Serbian Dinar (RSD)</option>

</select></td></tr>
<tr><td></td><td> <input id="btnConvert" type="button" value="Convert"  style="padding:5px;      10px;"/></td></tr>
</table>
<div >
<div id="result" style="padding: 2px; margin: 5px; font-size:20pt">
</div>
</div>
<br />
</form>


<!-- jQuery hosted by google version 2.0.3-->    
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

<script src="test_2.js"></script>

更新:我想我到了某个地方!当我使用开发人员工具时,获取URL现在是正确的......但它仍然无效。胡说            $(document).ready(function(){             $('#btnConvert')。click(function(){

        $('#result').html('Loading...');

    var amount = $('#amount').val();
    var from = $('#from').val();
    var to = $('#to').val();
    var value = [];

    $.ajax({
        type: "GET",
        url: "http://rate-exchange.appspot.com/currency?from=" + from + "&to=" + to + "&q=" + amount + "&callback=success",
        dataType: "jsonp",
        jsonp: false,
        cache: true,
        success: function success(resp) {
            $('#result').html(resp.v);

        }
    });
});
});


另一个更新 - 当我到达某个地方时......

$(document).ready(function () {
$('#btnConvert').click(function () {

    $('#result').html('Loading...');

    var amount = $('#amount').val();
    var from = $('#from').val();
    var to = $('#to').val();




    $.ajax({
        type: "GET",
        url: "http://rate-exchange.appspot.com/currency?from=" + from + "&to=" + to + "&q=" + amount + "&callback=function",
        dataType: "jsonp",
        jsonp: false,
        cache: true,
        success: function(resp) {
            document.getElementById('#result').innerHtml=resp.v;
        }


    });
});
});

因此,当我测试它时,我得到(在开发人员工具中)的响应:     function({“to”:“EUR”,“rate”:0.72894899999999996,“from”:“USD”,“v”:0.72894899999999996})

这是我想要的,但我不知道如何显示“v”...我一直在尝试resp.v或data.v但它不会显示......

3 个答案:

答案 0 :(得分:0)

这是你的ajax功能应该是什么样的 -

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

您不应在URL中指定回调函数名称。 Jquery会自动完成。

答案 1 :(得分:0)

首先,你的额外角色使你的请求无效:?$from应为?from

没有参数的

html()只获取html,你没有将它设置为任何东西。试试这个:

success: function(resp) {
    $('#result').html(resp.v);
}

如果这不是从resp中获取v property的正确方法,请尝试resp["v"]或其他内容。

答案 2 :(得分:0)

这很好。您可以尝试...

$('#result').val('Loading...');

var amount = $('#amount').val();
var from = $('#from').val();
var to = $('#to').val();



 $.ajax({
     type: "GET",
       url: "http://free.currencyconverterapi.com/api/v5/convert?q=" + from + "_" + to +"&compact=y",
    success: function(data) {
        var exchangeRate = JSON.stringify(data).replace(/[^0-9\.]/g,'');

        var result = amount*exchangeRate;
           $('#result').val(parseFloat(result).toFixed(2));
        }
    });

});