C:\Users\Administrator\Desktop\meteorApp>meteor add http
這是可以使用GET,POST,PUT和DELETE方法參數(shù)。在這個(gè)例子中,我們將向你展示如何使用GET參數(shù)。這一章中的例子將使用偽造REST API:訪問這個(gè)網(wǎng)站. 可以看到,這個(gè)方法是使用四個(gè)參數(shù)。我們已經(jīng)提到第一個(gè)參數(shù)GET。第二個(gè)是:API URL. 第三個(gè)參數(shù)是一個(gè)空的對(duì)象,在這里我們可以設(shè)置一些可選參數(shù)。最后一個(gè)方法是異步回調(diào),我們可以處理錯(cuò)誤,并響應(yīng)工作。
HTTP.call( 'GET', 'http://jsonplaceholder.typicode.com/posts/1', {}, function( error, response ) {
if (error) {
console.log(error);
} else {
console.log(response);
}
});
相同的請(qǐng)求還可以通過使用 GET 代替 CALL 方法發(fā)送。可以看到,第一個(gè)參數(shù)是現(xiàn)在API URL。
HTTP.get( 'http://jsonplaceholder.typicode.com/posts/1', {}, function( error, response ) {
if ( error ) {
console.log( error );
} else {
console.log( response );
}
});

在該方法中,我們?cè)O(shè)置需要被發(fā)送到服務(wù)器(postData)的數(shù)據(jù)作為第二個(gè)參數(shù)。在我們的 GET 請(qǐng)求與其他的都是一樣的。
var postData = {
data: {
"name1": "Value1",
"name2": "Value2",
}
}
HTTP.post( 'http://jsonplaceholder.typicode.com/posts', postData,
function( error, response ) {
if ( error ) {
console.log( error );
} else {
console.log( response);
}
});

var updateData = {
data: {
"updatedName1": "updatedValue1",
"UpdatedName2": "updatedValue2",
}
}
HTTP.put( 'http://jsonplaceholder.typicode.com/posts/1', updateData,
function( error, response ) {
if ( error ) {
console.log( error );
} else {
console.log( response );
}
});

var deleteData = {
data: {}
}
HTTP.del( 'http://jsonplaceholder.typicode.com/posts/1', deleteData,
function( error, response ) {
if ( error ) {
console.log( error );
} else {
console.log( response );
}
});
