简体   繁体   中英

Referencing data sent in an AJAX request

Suppose I am sending the following AJAX request using jQuery:

$.ajax({
    type: 'POST',
    async: true,
    url: '<?=$_SERVER['PHP_SELF'] ?>?date=<?=$date ?>',
    data: $('#dateform').serialize(),
    dataType: 'json',
    success: function (data) {
        alert('Successful for date ' + data['date']);
    },
    error: function (data) {
        alert('Error for date ' + data['date']);
    }
});

If the server passed back the 'data' element in the JSON, then all is good and it can be read for the alert.

However, if there was an error preventing a return of the date sent, how can the AJAX request detect which date value was sent within the error function? This is assuming the user selected a different date before the server had a chance to respond, so the date element in #dateform would no longer represent the date that was sent with the request.

在提交ajax帖子之前,将date的值保存到一个变量中,并在error函数中引用该变量的值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM