ajax请求接口数据

var api = 'http://192.168.68.208:666/ajax/api.ashx';
    // api += 'action=/api/blackhistory/list&keywords=&isPage=1&pageNo=1&pageCount=5'
// var api = 'test.txt';

// 获取异步数据
function ajax( callback ){        
    var req = new XMLHttpRequest();
    // req.open( 'get', api, true );
    req.open( 'post', api, true );

    req.onreadystatechange = function(){
        if( req.status === 200 && req.readyState === 4 ){
            // 调用回调函数
            callback( req.responseText );
        }
    }

    // 参数内容编码格式
    req.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );

    // console.log( req.getAllResponseHeaders() );

    // var param = {
    //     keywords: '',
    //     isPage: 1,
    //     pageNo: 1,
    //     pageCount: 5
    // }

    // var str = JSON.stringify( param );

    // 关于参数
    // 只有post支持
    // 只能放字符串
    // 参数格式与get方式一致

    var param = 'action=/api/blackhistory/list&keywords=&isPage=1&pageNo=1&pageCount=5';

    req.send( param );
}


// 处理函数
function test( data ){
    console.log( data );
}



ajax( test );

 

posted @ 2017-12-06 15:44  Sunsin  阅读(6970)  评论(0编辑  收藏  举报