使用 jQuery 发起 AJAX 请求并提交 JSON 数据的方法

评论259

 

<script>
$(document).ready(function() {
$.ajax({
    url: 'your_url_here',
    type: 'POST', // 或者 'GET'
    contentType: 'application/json',
    dataType: 'json',
    data: JSON.stringify({ key1: value1, key2: value2 }), // 将数据转换为 JSON 字符串
    success: function(response) {
        // 请求成功时的处理逻辑
        console.log('请求成功', response);
    },
    error: function(xhr, status, error) {
        // 请求失败时的处理逻辑
        console.error('请求失败', status, error);
    }
});
});
</script>

 

发表评论