我的jquery代码如下,这是一种通过 API.In json中的API数据转换的货币。我想通过 AJax 检索数据。
Im geting the error : Fatal error: Cannot access empty property in C:'xampp'htdocs'mvc'converter'ajax.php on line 25
<script type="text/javascript">
$(document).ready(function(){
$("#currency").change(function(){
var currency= $("#currency").val();
$.ajax({
type: "GET",
url: "ajax.php",
cache: false,
data: currency,
dataType: "text",
success: function(data){
alert(data);
$("#quantity").keyup(function(){
//var local_rate= $("#local_rate").val();
var quantity= $("#quantity").val();
var us_rate= quantity / cur;
$('.listprice').html(us_rate);
});
}
});
});
});
</script>
这是我的PHP(Ajax.php)代码
<?php
$currency =$_GET['currency'];
$file = 'latest.json';
$appId = '306bdd0f71fe465280e48188846534af';
// Open CURL session:
$ch = curl_init("http://openexchangerates.org/api/{$file}?app_id={$appId}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Get the data:
$json = curl_exec($ch);
curl_close($ch);
$exchangeRates = json_decode($json);
echo $rate= $exchangeRates->rates->$currency;
?>
我坚持检索数据,而没有使用 jquery 它完美地工作......在这里,我想通过Ajax加载。
将data: currency,
更改为data: {currency: currency},
,以便货币可以通过$_GET['currency']