익스프레스를 사용하여 JSON을 노드에서 직접 다른 서버에 게시 할 수있게하려고합니다. 기본적으로 다른 api를 호출 할 때 API 키를 노출하고 싶지 않지만 여전히 서비스를 호출 할 수 있습니다. 내가 원하는 클라이언트에서 https://github.com/GetResponse/DevZone/blob/master/API/examples/javascript_synopsis.html익스프레스에서 외부 서버로 JSON 게시
JS 구현할 :
var api_key = 'ENTER_YOUR_API_KEY_HERE';
// API 2.x URL
var api_url = 'http://api2.getresponse.com';
function add_contact() {
var campaigns = {};
// find campaign named 'test'
$.ajax({
url : api_url,
data : JSON.stringify({
'jsonrpc' : '2.0',
'method' : 'get_campaigns',
'params' : [
api_key,
{
// find by name literally
'name' : { 'EQUALS' : 'test' }
}
],
'id' : 1
}),
type : 'POST',
contentType : 'application/json',
dataType : 'JSON',
crossDomain : true,
async : false,
success : function(response) {
// uncomment following line to preview Response
// alert(JSON.stringify(response));
campaigns = response.result;
}
});
// because there can be only (too much HIGHLANDER movie) one campaign of this name
// first key is the CAMPAIGN_ID required by next method
// (this ID is constant and should be cached for future use)
var CAMPAIGN_ID;
for(var key in campaigns) {
CAMPAIGN_ID = key;
break;
}
$.ajax({
url : api_url,
data : JSON.stringify({
'jsonrpc' : '2.0',
'method' : 'add_contact',
'params' : [
api_key,
{
// identifier of 'test' campaign
'campaign' : CAMPAIGN_ID,
// basic info
'name' : 'Test',
'email' : '[email protected]',
// custom fields
'customs' : [
{
'name' : 'likes_to_drink',
'content' : 'tea'
},
{
'name' : 'likes_to_eat',
'content' : 'steak'
}
]
}
],
'id' : 2
}),
type : 'POST',
contentType : 'application/json',
dataType : 'JSON',
crossDomain : true,
async : false,
success : function(response)
{
// uncomment following line to preview Response
// alert(JSON.stringify(response));
alert('Contact added');
}
});
}
감사합니다.이 기능이 작동하는지 여부를 확인할 기회가 없었지만, 필자가 설명한 것과 정확히 똑같이하려고합니다. 요청 모듈이나 http://nodejs.org/api/http.html#http_http_request_options_callback을 사용해야합니까? 귀하가 지적한 요청 모듈의 이점은 무엇입니까? – BRogers
요청 모듈을 사용하는 것이 더 편리합니다. 사용을 권장합니다. – vmx
대단히 고마워! – BRogers