gpt4 book ai didi

javascript - 使用 JSONp 的货币转换器

转载 作者:行者123 更新时间:2023-11-28 11:44:39 26 4
gpt4 key购买 nike

我在尝试构建 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 现在是正确的......但它仍然无法正常工作。废话 $(文档).ready(函数(){ $('#btnConvert').click(函数 () {

        $('#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;
}


});
});
});

所以当我测试它时,我确实得到了(在开发者工具中)的响应: 函数({“至”:“欧元”,“汇率”:0.72894899999999996,“从”:“美元”,“v”:0.72894899999999996})

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

最佳答案

首先,你有一个额外的字符使你的请求无效:?$from 应该是 ?from

没有参数的

html() 只会获取 html,您不会将其设置为任何内容。试试这个:

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

如果这不是获得 v property 的正确方法在 resp 之外,尝试 resp["v"] 或其他东西。

关于javascript - 使用 JSONp 的货币转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20552845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com