如何使用ajax调用的响应来操作动态页面?

时间:2012-10-03 23:51:25

标签: ajax

我正在尝试使用用户插入的数据提交表单,并从名为(update.asp)的页面返回html。

  1. 如何获取html响应以及如何将其写入页面上的div?回应将是“成功”。
  2. 如果我的页面抛出500或其他类型的错误,我该如何处理?

    $('input#btnUpdate').click( function() {
        $.ajax({
        url: 'update.asp',
        type: 'post',
        dataType: 'json',
        data: $('form#myForm').serialize(),
        success: function(data) {
            // how do i catch the response? is this the right place?
        },
        error: function(data) {
            // how do I catch the error code here?
        }
    });
    

2 个答案:

答案 0 :(得分:0)

在两种情况下,服务器的响应都将作为示例中的数据变量传递给回调。尝试在回调中使用console.log(数据),以在开发人员控制台中查看结果。

答案 1 :(得分:0)

$('input#btnUpdate').click( function() {
    $.ajax({
        url: 'update.asp',
        type: 'post',
        dataType: 'json',
        data: $('#myForm').serialize(),
        success: function(response) {
              $("#yourDIV").html(response);
        },
        error: function (xhr, ajaxOptions, thrownError)  {
              alert(thrownError);        //output, 500
        }
    });
});

更多相关信息:ajax()