Ajax和jQuery对$Ajax url的调用不起作用

Ajax and jQuery call of $ajax url not working

本文关键字:Ajax 调用 不起作用 url jQuery      更新时间:2024-01-18

我会试着更好地解释它。

我通过ajax中的URL连接到RESTapi。当我得到这些信息时,我想更新里面的内容,这样内容就会根据从api调用中获得的数据进行更新。

<button onclick="myFunction()">LOAD</button><br /><br />
<div class="spinner bar hide">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div> 
<div class="searchtable"><?php include 'hotels/hotelList.php';?></div>
<script>
    function myFunction() { 
        $('.searchtable').addClass('hide');
        $('.spinner').removeClass('hide');
        $.ajax({
            url: 'hotels/hotelSortBy.php?name=<?php echo $name;?>&arrival=<?php echo $arrival;?>&departure=<?php echo $departure;?>&guests=<?php echo $numberOfGuests;?>'
        }).done(function() {
            $('.spinner').addClass('hide');
            $('.searchtable').removeClass('hide');
        });
    }
</script> 

问题在于您的ajax调用功能

你必须像下面的一样更改ajax调用

$.ajax({
            type: 'GET',
            data: {'name':'<?php echo $name;?>','arrival':'<?php echo $arrival;?>','departure':'<?php echo $departure;?>','guests':'<?php echo $numberOfGuests;?>'},
            url: 'hotels/hotelSortBy.php',
            success: function (data) {
                // do what ever you want to do with this response data
            },
            error: function (xhr) {
                // do what ever you want to do when error happens
            }
        });

这是使用ajax 向url发送GET请求的方法