我的大学小项目存在技术问题。教授给我的任务如下:
我们假设有静态表,其中包含3个域名,以提高域可用性API的性能。
-------------------------------------------------- |example1.com | $9.99 / year | Select Button | -------------------------------------------------- |example2.net | $12.99 / year | Select Button | -------------------------------------------------- |example3.org | $6.99 / year | Select Button | --------------------------------------------------
需要什么?
当我点击“选择按钮”时,它会将“example1.com”发送到API以检查其可用性,然后它成功函数返回1或0(1可用且0不可用)。 / p>
如果是1,我们需要将“选择按钮”更改为“已选择”
-------------------------------------------------- |example1.com | $9.99 / year | Selected | -------------------------------------------------- |example2.net | $12.99 / year | Select Button | -------------------------------------------------- |example3.org | $6.99 / year | Select Button | --------------------------------------------------
如果为0,我们需要将“选择按钮”更改为“不可用”
-------------------------------------------------- |example1.com | $9.99 / year | Not Available | -------------------------------------------------- |example2.net | $12.99 / year | Select Button | -------------------------------------------------- |example3.org | $6.99 / year | Select Button | --------------------------------------------------
其余部分也是如此,在检查完所有域名后,我们将最终得到最终表格,例如
-------------------------------------------------- |example1.com | $9.99 / year | Not Available | -------------------------------------------------- |example2.net | $12.99 / year | Selected | -------------------------------------------------- |example3.org | $6.99 / year | Selected | --------------------------------------------------
我的文件
的index.html
<table>
<tbody>
<tr>
<td class="domain">example1.com</td>
<td>$9.99 / year</td>
<td class="changebtn"><button type="button" class="btn btn-primary">Select</button></td>
</tr>
<tr>
<td class="domain">example2.net</td>
<td>$12.99 / year</td>
<td class="changebtn"><button type="button" class="btn btn-primary">Select</button></td>
</tr>
<tr>
<td class="domain">example3.org</td>
<td>$6.99 / year</td>
<td class="changebtn"><button type="button" class="btn btn-primary">Select</button></td>
</tr>
</tbody>
</table>
<script type="text/javascript">
$(document).on('click', '.btn-primary', function (e) {
e.preventDefault();
var domain = $(this).closest('tr').children('td.domain').text();
$.ajax({
type: "GET",
async: true,
url: "domainapi.php",
//`enter code here`
data: {
domain: domain
},
success: function (data) {
if (data == 1) {
/* change "Select Button" to "Selected"*/
} else {
/* change "Select Button" to "Not Available"*/
}
}
});
});
</script>
domainapi.php
$domain = $_GET["domain"];
$checkdomain = "https://api.example.com/xml.response?&Domain=$domain";
$data = file_get_contents($checkdomain);
$xml = simplexml_load_string($data);
if( ! $xml)
{echo "unable to load XML file";}
else
{
foreach ($xml->CommandResponse->DomainCheckResult as $result) {
$checkavailablity = $result["Available"];
$checkprice = $result["PremiumRegistrationPrice"];
if (($checkavailablity == "true") && ($checkprice == "0"))
{echo 1;}
/*Ajax success: function(data) will return 1 and then we decided in the above success function to change the "Select Button" to "Selected"*/
else {echo 0;}
/*Ajax success: function(data) will return 0 and then we decided in the above success function to change the "Select Button" to "Not Available"*/
}
}
答案 0 :(得分:0)
您的API是否返回了正确的值?
我不确定它是否有效。
var index = $('.btn-primary').index(this);
if (data == 1) {
/* change "Select Button" to "Selected"*/
$('.btn-primary:eq(' + index + ')').html('Selected');
} else {
/* change "Select Button" to "Not Available"*/
$('.btn-primary:eq(' + index + ')').html('Not Available');
}