$.ajax的beforeSend,success, complete,error例子

zhuxiongxian發表於2016-11-16

jquery ajax官方文件: http://api.jquery.com/jquery.ajax/

常用的ajax形式:

$.ajax({
    url: "http://192.168.2.46:8000/account/getjson/",
    type: "post",
    dataType: "json", // 跨域使用jsonp
    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
    data: {
        "user": "admin",
        "password": "123456"
    },
    beforeSend: function(XMLHttpRequest) {
        // do something...
        return true;
    },
    success: function(data) {
        // alert(JSON.stringify(data));
        // do something...
    },
    complete: function(XMLHttpRequest, textStatus) {
        // textStatus的值:success, notmodified, nocontent, error, timeout, abort, parsererror  
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        // textStatus的值:null, timeout, error, abort, parsererror
        // errorThrown的值:收到http出錯文字,如 Not Found 或 Internal Server Error
    }
});

相關文章